arrange indices

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

 采纳的回答

[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 个评论

thanks,
this is what i need , but what i have to do if need vector as:
r=[5 6 3 4 2 4];
i.e 15, 16, 18 are deleted
thanks in advance
out(3:3:end) = []
thanks,
but when try it with vector with length 8
i get error
ex.
s1=[1 3 4 5 62 3 5 7 ];
>> out(i,:) = reshape(sortrows(reshape(s1,3,[])',3)',1,[]);
??? Error using ==> reshape
Product of known dimensions, 3, not divisible into
total number of elements, 8.
what i ahve to do in this case?
You violated your original description, where you had a multiple of three elements and you sorted the "third" ones, and moved them around to their new location, taking the prior two elements with them to their new locations. Now with 8 elements you have an extra two elements (at indexes 7 and 8) that don't have a third element associated with them at index 9. Please think about what you want to do in general and then share that.
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
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,[]);
out = [r(1),reshape(sortrows(reshape(r(2:end),3,[])',3)',1,[])];

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by