How can I get subscript instead in indices from sort() ??

조회 수: 1 (최근 30일)
JAI PRAKASH
JAI PRAKASH 2018년 8월 7일
댓글: Fangjun Jiang 2018년 8월 7일
How can I get subscript instead in indices from
sort(mat(:));
Script:
%%Creation of 'mat'
mat = magic(100);
for i=1:100*100
mat(mat>(100*i-100) & mat<=100*i)=i;
end
[~,idx] = sort(mat(:));
%%I dont want to use 'for' loop. I.e.,
for i=1:length(idx) % I want to avoid for loop
[I(i),J(i)] = ind2sub(size(mat),idx(i));
end
I tried 'arrayfun' also but it is taking more time then for loop
[I2,J2] = arrayfun(@(x) ind2sub(size(mat),x),idx);
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2018년 8월 7일
mat(:) turns mat() into a column vector. No point to get indices anyway.

댓글을 달려면 로그인하십시오.

채택된 답변

jonas
jonas 2018년 8월 7일
편집: jonas 2018년 8월 7일
ind2sub accepts multiple indices as input. It's right there in the first example of the doc.
IND = [3 4 5 6]
s = [3,3];
[I,J] = ind2sub(s,IND)
  댓글 수: 1
JAI PRAKASH
JAI PRAKASH 2018년 8월 7일
OMG what a silly question I asked.
Thanx for ur exact help

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by