Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

swap a matrix using two vectors

1 Ansicht (letzte 30 Tage)
Anusha
Anusha am 24 Apr. 2014
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
an =
2
3
5
6
0
1
4
bn =
4
5
1
3
2
6
0
I need to swap these two vectors in the matrix b =
4 3 2 5
1 0 6 2
3 2 5 1
  1 Kommentar
Anusha
Anusha am 24 Apr. 2014
all the 2 becomes 4 ,3 becomes 5,5 becomes 1.,etc

Antworten (1)

the cyclist
the cyclist am 24 Apr. 2014
[~,idx] = ismember(b,an)
b_swapped = bn(idx)
  3 Kommentare
Anusha
Anusha am 24 Apr. 2014
only direct swap only. 2 to 4 and 4 to 0 directly.. no swap 2 to 0
the cyclist
the cyclist am 24 Apr. 2014
My algorithm does direct swap only. Run this code:
an = [2 3 5 6 0 1 4];
bn = [4 5 1 3 2 6 0];
b = [4 3 2 5; ...
1 0 6 2; ...
3 2 5 1];
[~,idx] = ismember(b,an);
b_swapped = bn(idx)
I get
b_swapped =
0 5 4 1
6 2 3 4
5 4 1 6
Isn't this exactly what you want?

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by