error "Out of range subscript"??

25 次查看(过去 30 天)
Sahar abdalah
Sahar abdalah 2015-7-6
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.

回答(1 个)

Walter Roberson
Walter Roberson 2015-7-6
[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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by