error "Out of range subscript"??

17 Ansichten (letzte 30 Tage)
Sahar abdalah
Sahar abdalah am 6 Jul. 2015
Beantwortet: Walter Roberson am 6 Jul. 2015
I have two matrix: decision.mat and order.mat. the order matrix contains labels and the decision matrix contains indices of element and I want to obtain a matrix result that corresponding to the values in order according to the indices of the matrix decision. there is an example :
order=[1;25;6;5;2];
decision=[1;2;5;4;3];
resultat=[1;25;2;5;6];
I use this code to obtain but I got this error :
[n,m]=size(decision);
resultat=reshape( order(sub2ind([n,m],(1:n)'*ones(1,m),decision)),n,m);
Error using sub2ind (line 52)
Out of range subscript.
Error in ex2 (line 7)
resultat=reshape( order(sub2ind([n,m],(1:n)'*ones(1,m),decision)),n,m);
please help me to solve this problem and thanks in advance.

Antworten (1)

Walter Roberson
Walter Roberson am 6 Jul. 2015
[n,m] = size(decision) is going to give n = 5, m = 1. ones(m,1) is then going to be the scalar 1. (1:n)' is going to be [1;2;3;4;5] and * 1 that is going to give the same. So now you are trying to sub2ind([5,1], [1;2;3;4;5], [1;2;5;4;3]) . That would require that [1,1], [2, 2], [3, 5], [4, 4], [5, 3] be valid subscripts of a 5 x 1 vector. Only the first of those is valid.
I do not know what you are trying to do there. I suspect you are trying to permute order by rows. If so then just
order(decision,:)
would be enough.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by