Rearrange matrix elements accordi a given set of data
Show older comments
Hello, Maybe it's a basic and obvious problem to solve, but right now I can't get out. I would like to rearrange matrix elements according to a specified set of data in another matrix. As example, I have a mtrix of positions given by the "sort" command, let's say
a = 3 2 5
6 1 4
1 2 3
[b index]=sort(a,1);
index =
3 2 3
1 1 2
2 3 1
Now, I would lke to use the index matrix to rearrange elements of the matrix c:
c =
1 2 3
2 3 4
3 4 5
so the output should be
c =
3 3 5
1 2 4
2 4 3
Is there a command to do that, or something similar? Any hint would be very appreciated.
Best regards,
Luca.
Answers (1)
Azzi Abdelmalek
on 23 Apr 2013
Edited: Azzi Abdelmalek
on 23 Apr 2013
d=cell2mat(arrayfun(@(x) c(index(:,x),x),1:size(c,2),'un',0))
%or
a = [3 2 5
6 1 4
1 2 3]
[b index]=sort(a,1)
c= [ 1 2 3
2 3 4
3 4 5]
n=size(index,1)
index=bsxfun(@plus,index,0:n:(n-1)*n)
d=c(index)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!