arrange indices

조회 수: 1 (최근 30일)
huda nawaf
huda nawaf 2012년 4월 28일
hi, if i have this vector
r=[2 4 18 5 6 15 3 4 16]; i need arrange index 3,6,9 with ascending, and each both previous index are arranged according for that as:
r=[5 6 15 3 4 16 2 4 18]; i.e just 3,6,.....are arranged with ascending and the index befor it are arranged according to third , sixth,..... i would to say ,this is just example i have too long vector

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 4월 28일
[ii,ii] = sort(r(3:3:end));
out = reshape(r(bsxfun(@plus,ii*3,(-2:0)')),1,[]);
OR:
out = reshape(sortrows(reshape(r,3,[])',3)',1,[]);
  댓글 수: 7
huda nawaf
huda nawaf 2012년 4월 28일
what if need that
ex. r=[1 2 4 18 5 6 15 3 4 16];
if need 1 in same index and the other values as:
r=[1 5 6 15 3 4 16 2 4 18];
what will be changed in this command?
out = reshape(sortrows(reshape(r,3,[])',3)',1,[]);
Andrei Bobrov
Andrei Bobrov 2012년 4월 29일
out = [r(1),reshape(sortrows(reshape(r(2:end),3,[])',3)',1,[])];

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by