Sorting a Matrix using Indices from another matrix !

Hello
I have a pretty simple question !
I have a matrix A (lets assume has a size 5 x 5) and I have another matrix of same size (5 x 5) called IDX that contains the values for sorting A in a colum-wise manner.
So how can I easily sort each column of A using matrix IDX without using a for loop or extracting each column of IDX as an individual vector ?
Thank you in advance !

2 comentarios

the cyclist
the cyclist el 13 de Sept. de 2013
This would be much easier to answer if you gave examples of the two matrices, and the expected result.
Dimitris M
Dimitris M el 13 de Sept. de 2013
Editada: Image Analyst el 13 de Sept. de 2013
Ok sure
A = [1 1 1 0 0 0;
0 0 0 1 1 1;
0 1 0 1 0 1]'
IDX = [4 5 6 3 2 1;
1 2 3 6 5 4;
6 5 4 3 2 1]'
And I want to sort A using the indices of IDX in a columnwise manner!
Is it clear enough now?

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 13 de Sept. de 2013
[m,n]=size(A);
A=A(sub2ind([m n],idx,repmat(1:n,m,1)))

5 comentarios

Perhaps I misunderstood what you want, because Azzi's and my code give completely different results. I thought you wanted to take each column of IDX to be the sort order for the corresponding column of A. So that if IDX was
IDX =
4 1 6
5 2 5
6 3 4
3 6 3
2 5 2
1 4 1
as you gave it. So then the result should be
new A=
A(4,1) A(1, 2) A(6, 3)
A(5,1) A(2, 2) A(5, 3)
A(6,1) A(3, 2) A(4, 3)
A(3,1) A(6, 2) A(3, 3)
A(2,1) A(5, 2) A(2, 3)
A(1,1) A(4, 2) A(1, 3)
where you take the A from the row where IDX told you to, in a column-by-column basis. You rxample wasn't good because with so many 0's and 1's, it's hard to tell what went where. However if
A = [1:6;7:12;13:18]'
A =
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18
Then my code gives
A =
4 7 18
5 8 17
6 9 16
3 12 15
2 11 14
1 10 13
while Azzi's gives:
A =
3 7 13
2 8 14
1 9 15
6 10 16
5 11 17
4 12 18
As you can see they're totally different. My columns of A are re-ordered according to the numbers in the columns of IDX. I can't really figure out what algorithm Azzi's follows. True, mine uses a for loop while his doesn't but you shouldn't be afraid of for loops for arrays this tiny - it's only a factor when you get up in to the tens of millions of iterations, not just 3 iterations. But whatever - if his gives you the order that you want, then fine, I must have misunderstood.
Image Analyst, The results are the same
IDX =[4 1 6;5 2 5;6 3 4;3 6 3;2 5 2;1 4 1]
A = [1:6;7:12;13:18]'
%----------Azzi's code------------------
[m,n]=size(A);
A1=A(sub2ind([m n],idx,repmat(1:n,m,1)))
%---------Analyst's code----------------
[rows, columns] = size(A)
% Sort columns of A according to the same column of IDX
for col = 1 : columns
A(:,col) = A(IDX(:,col), col);
end
%-------------------------------------------------------
isequal(A1,A)
Image Analyst
Image Analyst el 13 de Sept. de 2013
Sorry - you're right. I ran your code after mine and forgot to set A back to the original A. I still don't follow what yours does, but it works though.
idx =[4 1 6;5 2 5;6 3 4;3 6 3;2 5 2;1 4 1];
A = [1:6;7:12;13:18]';
idx1=idx % indicate line index
4 1 6
5 2 5
6 3 4
3 6 3
2 5 2
1 4 1
[m,n]=size(A);
idx2=repmat(1:n,m,1) %indicate column index
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
idx=sub2ind([m n],idx1,idx2) % to get linear index
4 7 18
5 8 17
6 9 16
3 12 15
2 11 14
1 10 13
A=A(idx)
Aaron Thode
Aaron Thode el 31 de Jul. de 2018
Very impressive; I've wondered about this for a long time. Thank you!

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 13 de Sept. de 2013
Try this:
% Create random sample data so it will be easy
% for us to see if the sorting worked.
A = randi(9, [6,3])
IDX = [4 5 6 3 2 1;
1 2 3 6 5 4;
6 5 4 3 2 1]'
[rows, columns] = size(A)
% Sort columns of A according to the same column of IDX
for col = 1 : columns
A(:,col) = A(IDX(:,col), col);
end
% Print out to command window.
A
In the command window:
A =
7 7 7
3 8 7
9 2 3
1 5 7
4 5 6
4 6 2
IDX =
4 1 6
5 2 5
6 3 4
3 6 3
2 5 2
1 4 1
rows =
6
columns =
3
A =
1 7 2
4 8 6
4 2 7
9 6 3
3 5 7
7 5 7
Azzi Abdelmalek
Azzi Abdelmalek el 13 de Sept. de 2013
Editada: Azzi Abdelmalek el 13 de Sept. de 2013
Edit2
A = [1 1 1 0 0 0; 0 0 0 1 1 1; 0 1 0 1 0 1]'
idx= [4 5 6 3 2 1; 1 2 3 6 5 4; 6 5 4 3 2 1]'
[m,n]=size(A);
idx=bsxfun(@plus,idx,(0:m:(n-1)*m))
A=A(idx)

Categorías

Preguntada:

el 13 de Sept. de 2013

Comentada:

el 31 de Jul. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by