MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by huda nawaf on 28 Apr 2012

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

*No products are associated with this question.*

Answer by Andrei Bobrov on 28 Apr 2012

Accepted answer

[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,[]);

Show 4 older comments

huda nawaf on 28 Apr 2012

i want arrange the third , sixth, ninth,....elements with ascending , but when these elemnts are arranged the other elements that correspond with it it must be arranged also according to the

as example above

r=[2 4 18 5 6 15 3 4 16];

it must be as:

r=[5 6 15 3 4 16 2 4 18]

thanks

huda nawaf on 28 Apr 2012

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,[]);

## 0 Comments