I have two arrays A1 and B1 in which elements correspond:
A1 = [a1 a2 a3 a4 a5];
B1 = [b1 b2 b3 b4 b5];
then I change order of elements of the first array anyhow (they are all unique), for exaple like this:
A2 = [a4 a1 a3 a5 a2];
The question is how to get array B2 sorted in the same way? It should be
B2 = [b4 b1 b3 b5 b2];
I tried to write function which is:
B2 = zeros(length(B1),1);
for i = 1:length(B2)
ind = find( A1 == A2(i) );
B2(i) = B1(ind);
end
but as it uses for loops speed is not fast. Maybe there is a way to do it by using MatLab builtin functions?

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 23 de Mayo de 2018
Editada: Ameer Hamza el 23 de Mayo de 2018

2 votos

For a generalized matrix a1 and a2, you can use the following code
[~,index] = ismember(a2', a1', 'row');
b2 = b1(index)
This will work on R2016b and later.

8 comentarios

It is absolutelly correct! Thank you! However it is not faster than code with for loops for 4e4 elements :)
b2 = zeros( length(b1), 1 );
for i = 1:length(b2)
ind = find( all(a1 == a2(i,:), 2) );
b2(i) = b1(ind);
end
Ameer Hamza
Ameer Hamza el 23 de Mayo de 2018
You didn't mention the size of your matrices. The slowdown is happening because of huge logical matrices creation. For your case, try the updated answer.
Your code is even slower. But this:
index = sparse(a1(1,:) == a2(1,:)' & a1(2,:) == a2(2,:)')*(1:length(b1))'
b2 = b1(index);
make it faster. Not really significantly but faster for sure. Thank you again
Ameer Hamza
Ameer Hamza el 23 de Mayo de 2018
Have you tried the updated answer?
Rostislav Teryaev
Rostislav Teryaev el 23 de Mayo de 2018
WOOOOW! Such elegant) Yes this solution is the best one. It is instant for 4e4 elements.
Ameer Hamza
Ameer Hamza el 23 de Mayo de 2018
You are welcome.
Rostislav Teryaev
Rostislav Teryaev el 23 de Mayo de 2018
How did you train yourself to invent such elegant solutions?
Ameer Hamza
Ameer Hamza el 23 de Mayo de 2018
You will get hold of such solution once you start to develop some intuition about these frequently used MATLAB functions. Of course, that comes with time, experience and reading a lot of documentation :)

Iniciar sesión para comentar.

Más respuestas (1)

sloppydisk
sloppydisk el 23 de Mayo de 2018

3 votos

a1 = [1 4 2 5 7];
b1 = 1:5;
mySecretOrder = [1 3 2 5 4];
a2 = a1(mySecretOrder);
[~, order] = ismember(a2, a1)
b1 = b1(order)

2 comentarios

nice! But what if the a1 and a2 are matrices? Real example:
a1 = [5 2 3 4 2 3 6 6 6
1 1 1 3 4 5 4 5 2]
b1 = [19 28 19 38 24 20 27 11 16]
a2 = [2 2 3 3 4 5 6 6 6
1 4 1 5 3 1 2 4 5]
b2 = [28 24 19 20 38 19 16 27 11]
There are columns (which stand for directions from 5 to 1, from 2 to 1 and so on) then they change their order in matrix and become a2. So I need to sort b2 which is also in the order of a1 columns and turn it into b2 which is in a2 order.
Sorry for asking not very this question in the first time. I supposed I would manage to extrapolate solution of easy case for more complicated case like this.
sloppydisk
sloppydisk el 23 de Mayo de 2018
Please see Ameer's answer.

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 23 de Mayo de 2018

Comentada:

el 23 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by