简体   繁体   English

如何操作二维数组/矩阵的每个单元?

[英]How to opperate each cell of 2D array/matrix?

Hi I'm reading two rasters A & B as arrays.嗨,我正在阅读两个光栅AB作为 arrays。

What I'm looking for is to make an opperation to certain cells within two 2D arrays (two rasters).我正在寻找的是对两个 2D arrays(两个栅格)内的某些单元格进行操作。 I need to subtract -3.0 to the cells in one array (A) that are greater than the other cells within the 2D array (B) .我需要将一个数组(A)中大于二维数组(B)中其他单元格的单元格减去 -3.0。

All the other cells don't need to change, so my answer will be the 2D array (B) with some changed cells that fit that condition and the other 2D array (A) untouched.所有其他单元格都不需要更改,因此我的答案将是 2D 数组(B) ,其中包含一些符合该条件的已更改单元格,而其他 2D 数组(A)则保持不变。

I tried this but doesn't seem to work (also takes TOO long):我试过了,但似乎没有用(也需要太长时间):

A = Raster_A.GetRasterBand(1).ReadAsArray()
B = Raster_B.GetRasterBand(1).ReadAsArray()

A = array([ 917.985028, 916.284480, 918.525323, 920.709505,
            921.835315, 922.328555, 920.283029, 922.229594,
            922.928670, 925.315534, 922.280360, 922.715303,
            925.933969, 925.897328, 923.880606, 923.864701])

B = array([ 913.75785758,  914.45941854,  915.17586919,  915.90724705,
            916.6534542 ,  917.4143068 ,  918.18957846,  918.97902532,
            919.78239295,  920.59941086,  921.42978108,  922.27316565,
            923.12917544,  923.99736194,  924.87721232,  925.76814782])

for i in np.nditer(A, op_flags=['readwrite']):
    for j in np.nditer(B, op_flags=['readwrite']):
        if j[...] > i[...]:
            B = j[...]-3.0

So the answer, the array B should be something like:所以答案,数组B应该是这样的:

B = array([ 913.75785758,  914.45941854,  915.17586919,  915.90724705,
            916.6534542 ,  917.4143068 ,  918.18957846,  918.97902532,
            919.78239295,  920.59941086,  921.42978108,  922.27316565,
            923.12917544,  923.99736194,  921.87721232,  922.76814782]) 

Please notice the two bottom right values :)请注意右下角的两个值:)

I'm a bit dizzy already from trying and doing other stuff at the same time so I apologize if I did any stupidity right there, any suggestion is greatly appreciated.同时尝试和做其他事情让我有点头晕,所以如果我在那里做了任何愚蠢的事情,我很抱歉,任何建议都非常感谢。 Thanks!谢谢!

Based on your example, I conclude that you want to subtract values from the array B .根据您的示例,我得出结论,您想从数组B中减去值。 This can be done via这可以通过

B[A<B] -= 3

The "mask" A<B is a boolean array that is true at all the values that you want to change. “掩码” A<B是一个 boolean 数组,对于您要更改的所有值都是正确的。 Now, B[A<B] returns a view to exactly these values.现在, B[A<B]将视图返回到这些值。 Finally, B[A<B] -= 3 changes all these values in place.最后, B[A<B] -= 3改变了所有这些值。

It is crucial that you use the inplace operator -= , because otherwise a new array will be created that contain only the values where A<B .使用就地运算符-=至关重要,否则将创建一个仅包含A<B值的新数组。 Thereby, the array is flattened, ie looses its shape, and you do not want that.因此,阵列被展平,即失去其形状,您不希望这样。

Regarding speed, avoid for loops as much as you can when working with numpy.关于速度,在使用 numpy 时尽可能避免循环。 Fancy indexing and slicing offers you very neat (and super fast) options to work with your data.花式索引和切片为您提供了非常简洁(且超快速)的选项来处理您的数据。 Maybe have a look here and here .也许看看这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM