В сеансе октавы:
>> OP = reshape(1:16,4,4);
>> OP
OP =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
>> mask = logical([1,0,0,1;0,1,1,1;0,0,0,0;1,0,1,0])
mask =
1 0 0 1
0 1 1 1
0 0 0 0
1 0 1 0
>> OP(mask)
ans =
1
4
6
10
12
13
14
Равель не имеет значения:
>> OP(mask(:))
ans =
1
4
6
10
12
13
14
В сеансе ipython/numpy
:
In [368]: OP = np.arange(1,17).reshape(4,4)
In [369]: OP
Out[369]:
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
In [370]: mask = np.array([[1,0,0,1],[0,1,1,1],[0,0,0,0],[1,0,1,0]]).astype(bool)
In [371]: mask
Out[371]:
array([[ True, False, False, True],
[False, True, True, True],
[False, False, False, False],
[ True, False, True, False]])
In [372]: OP[mask]
Out[372]: array([ 1, 4, 6, 7, 8, 13, 15])