开发者

Finding Max Question

I have such a list List<Double[,]>. Let's call each 2-dimensional array in the list a layer. So I should compare each element in each layer and extract max. An开发者_高级运维d construct layer of max values.

How do I do that? Maybe with use of LINQ? Or foreach loop construction?

Help!

And Thanks!


var x = new double[,] { { 1, 2 }, { 3, 4 } };
var y = new double[,] { { 5, 6 }, { 7, 8 } };
var list = new List<double[,]> { x, y };

var maxValues = list
    .Select(arg => arg.Cast<double>().Max())
    .ToList();

So as I understand x and y are levels.
The the result will be 4 and 8, which are max on level x and y respectively.

[Edit]

Seems like I misunderstood the question. To find the level with max you can use code like this:

var maxLevel = list
    .Select(arg => new { Max = arg.Cast<double>().Max(), Level = arg })
    .OrderByDescending(arg => arg.Max)
    .Select(arg => arg.Level)
    .First();


Assuming that all your layers are the same size sizeXxsizeY, because otherwise this makes no sense:

var maxLayer = new Double[sizeX,sizeY];

for( int x = 0; x <= maxLayer.GetUpperBound(0); x++ )
    for( int y = 0; y <= maxLayer.GetUpperBound(1); y++ )
        maxLayer[x,y] = Double.NegativeInfinity;

foreach( Double[,] layer in list )
    for( int x = 0; x <= maxLayer.GetUpperBound(0); x++ )
        for( int y = 0; y <= maxLayer.GetUpperBound(1); y++ )
            maxLayer[x,y] = Math.Max( maxLayer[x,y], layer[x,y] );

Nothing clever here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜