error "Out of range subscript"??
조회 수: 12 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!