C# - 访问一个多维数组的一个片段

时间:2016-04-19 08:58:40

标签: c# arrays matrix

假设a有一个MxNx3矩阵

byte [,,] myMatrix= new byte[sizeRow, sizeCol, 3];

如何访问它的单个频段(用于读写目的)?类似的东西:

singleBand = myMatrix[:allRows: , :allCols: , :desiredBand:];

On the left is what I have, of the right is what I want to access (for example.)

左边是我所拥有的,右边是我想要访问的内容(例如)。

3 个答案:

答案 0 :(得分:0)

int M=10;
int N=20;
var test = new byte[3][,] { new byte[N,M],new byte[N,M],new byte[N,M]};
var band1 = test[1]; //its green
band1[2, 2] = 99;

答案 1 :(得分:0)

如果您无法更改myMatrix的类型,则可以使用以下代码:

byte [,,] myMatrix= new byte[sizeRow, sizeCol, 3];
var singleBand = new byte[sizeRow, sizeCol];
var band = 1;

for (var i = 0; i < sizeRow; i++) {
    for (var j = 0; j < sizeCol; j++) {
        singleBand[i, j] = myMatrix[i, j, band];
    }
}

但如果你可以改变它,那么可能Zeromus的解决方案更好,因为你可以更容易地操控你所谓的乐队。

答案 2 :(得分:0)

您只需循环遍历所需的数组元素并提取所需的“乐队”。

{
  "_index": "logstash-2016.04.19",
  "_type": "syslog",
  "_id": "AVQt7WSCN-2LsQj9ZIIq",
  "_score": null,
  "_source": {
    "message": "212.95.71.201 \"-\" - - [19/Apr/2016:11:50:12 +0200] \"GET http://monsite.com/api/opa/status HTTP/1.1\" 200 92 \"-\" \"curl - API-Player - PREPROD\" hit OPA-PREPROD-API - 0.000132084",
    "@version": "1",
    "@timestamp": "2016-04-19T09:50:12.000Z",
    "type": "syslog",
    "host": "212.95.70.80",
    "tags": [
      "_grokparsefailure"
    ],
    "application": "varnish-preprod",
    "clientip": "1.2.3.4",
    "x_forwarded_for": "-",
    "ident": "-",
    "auth": "-",
    "timestamp": "19/Apr/2016:11:50:12 +0200",
    "verb": "GET",
    "request": "http://monsite.com/api/opa/status",
    "httpversion": "1.1",
    "response": "200",
    "bytes": "92",
    "referrer": "\"-\"",
    "agent": "\"curl - API-Player - PREPROD\"",
    "hitmiss": "hit",
    "varnish_conf": "OPA-PREPROD-API",
    "varnish_backend": "-",
    "time_firstbyte": "0.000132084",
    "geoip": {
      "ip": "1.2.3.4",
      "country_code2": "FR",
      "country_code3": "FRA",
      "country_name": "France",
      "continent_code": "EU",
      "region_name": "C1",
      "city_name": "Strasbourg",
      "latitude": 48.60040000000001,
      "longitude": 7.787399999999991,
      "timezone": "Europe/Paris",
      "real_region_name": "Alsace",
      "location": [
        7.787399999999991,
        48.60040000000001
      ]
    },
    "agentname": "Other",
    "agentos": "Other",
    "agentdevice": "Other"
  },
  "fields": {
    "@timestamp": [
      1461059412000
    ]
  },
  "highlight": {
    "agent": [
      "\"curl - API-Player - @kibana-highlighted-field@PREPROD@/kibana-highlighted-field@\""
    ],
    "varnish_conf": [
      "OPA-@kibana-highlighted-field@PREPROD@/kibana-highlighted-field@-API"
    ],
    "application": [
      "@kibana-highlighted-field@varnish@/kibana-highlighted-field@-@kibana-highlighted-field@preprod@/kibana-highlighted-field@"
    ],
    "message": [
      "1.2.3.4 \"-\" - - [19/Apr/2016:11:50:12 +0200] \"GET http://monsote.com/api/opa/status HTTP/1.1\" 200 92 \"-\" \"curl - API-Player - @kibana-highlighted-field@PREPROD@/kibana-highlighted-field@\" hit OPA-@kibana-highlighted-field@PREPROD@/kibana-highlighted-field@-API - 0.000132084"
    ]
  },
  "sort": [
    1461059412000
  ]
}

您唯一的选择是一次访问一个乐队中的每个“单元格”并将其提取到其他位置。在我的情况下,我只是把它们放到显示器上。