1

I'd like to copy data from one 3D array to another 3D array at the indices where a condition is true for a different 2D array. All three arrays have the same first two dimensional shapes (x,y coords).

I thought it'd be something like,

a[c == cond] = b[c == cond]

But in this case it is resulting in corrupted/garbled data when inspected. Is this the wrong way to go about this or is this the correct way and there is a problem with my code somewhere else?

Thanks!

4

3 回答 3

2

如果您使用的是新的 numpy 版本,请使用np.copyto.

于 2013-03-31T15:16:13.883 回答
1

如果数组具有完全相同的形状,那么您可以这样做

import numpy as np
a = np.random.rand(4,5,3)
b = np.random.rand(4,5,3)
c = np.random.rand(4,5,3)
cond = c > 0.5 # for example
b[cond] = a[cond]

但是,如果形状在最后一个轴上不同,那么您需要解释您期望发生的情况。

于 2013-03-31T09:51:24.723 回答
0

抱歉,我复制到的数组具有不同的 dtype,它正在转换为该数组的 dtype,而不是将其转换为原始 dtype。通过使用与源数组相同的 dtype 初始化该数组来修复。

于 2013-04-01T03:07:44.990 回答