I'm trying to replicate the following code from MATLAB in Python:
X = [1,2,3;
4,5,6];
idx = [2,1];
idy = [3,2];
x = X(idx,idy)
Output:
x =
6 5
3 2
I wish it was as simple as:
X = np.array([[1,2,3],[4,5,6]])
idx = np.array([1,0])
idy = np.array([2,1])
x = X[idx,idy]
but that is not the case.
What am I missing?
Second Example:
X = [0.677888793318259 0.141577665623206 0.175278629658347 0.00525491140018830
0.684907327765215 0.137981388019339 0.172732191381956 0.00437909283349025
0.691925862212172 0.134385110415471 0.170185753105565 0.00350327426679220
0.698944396659129 0.130788832811603 0.167639314829174 0.00262745570009415
0.705962931106086 0.127192555207735 0.165092876552782 0.00175163713339610
0.712981465553043 0.123596277603868 0.162546438276391 0.000875818566698050
0.720000000000000 0.120000000000000 0.160000000000000 0]
idx = [1 1 1 1 1 1 0]
idy = [1 1 1 1]
Correct Output:
x = [0.677888793318259 0.141577665623206 0.175278629658347 0.00525491140018830
0.684907327765215 0.137981388019339 0.172732191381956 0.00437909283349025
0.691925862212172 0.134385110415471 0.170185753105565 0.00350327426679220
0.698944396659129 0.130788832811603 0.167639314829174 0.00262745570009415
0.705962931106086 0.127192555207735 0.165092876552782 0.00175163713339610
0.712981465553043 0.123596277603868 0.162546438276391 0.000875818566698050]
x = X[idx,:][:,idy]idxandidxare logical arrays (containing Boolean values). That is a distinctly different way of indexing than what you had here before. You can convert the logical arrays into indices usingnp.nonzero.