rearranging the ind2sub result

I used [r,c] = ind2sub(size(matrix),idx) and the result gives me something like:
[r,c] =
1 1
2 1
4 1
5 1
1 2
3 2
4 2
5 2
etc.
It is arranged increasingly in the COLUMN part. Is it possible to do the opposite? (Increasing in the ROW part?)
Thanks

답변 (1개)

Guillaume
Guillaume 2015년 11월 10일
편집: Guillaume 2015년 11월 10일

0 개 추천

You can't get ind2sub to return values in a different order but you can sort the values afterward:
[r, c] = ind2sub(size(m), idx);
[r, order] = sort(r, 'stable');
c = c(order);
Another option is to put both r and c in the same matrix and use sortrows.
[rc(1), rc(2)] = ind2sub(size(m), idx);
rc = sortrows(rc);

댓글 수: 2

cgo
cgo 2015년 11월 10일
'stable' doesn't seem to work. But it worked when I used 'ascend'. Thank you for your.
Guillaume
Guillaume 2015년 11월 10일
Sorry, I forgot that it's only unique that has a 'stable' option. Possibly, sort is already stable. In any case, 'ascend' is the default, so you don't need the 2nd argument.
Note that if sort is not stable (i.e. the order of c is not preserved for identical r), then the sortrows option is guaranteed to be.

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

카테고리

도움말 센터File Exchange에서 Parallel Computing Toolbox에 대해 자세히 알아보기

태그

질문:

cgo
2015년 11월 10일

댓글:

2015년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by