Using a matrix as an index of another matrix

1 Ansicht (letzte 30 Tage)
Amine Alrharad
Amine Alrharad am 10 Mär. 2023
Kommentiert: Amine Alrharad am 10 Mär. 2023
Hello everybody, I need some help please!
I am trying to sort a matrix (x) and go back to the original order based on the index matrix (idx2).
a = 30.0;
b = 100.0;
for i=1:5
x = (b-a).*rand(5,5) + a;
x = round(x,1);
end
[y, idx2] = sort(x, 2);
Thank you in advance

Antworten (1)

Steven Lord
Steven Lord am 10 Mär. 2023
Take some shuffled data.
r = randperm(10)
r = 1×10
4 9 6 2 1 5 7 8 3 10
Now sort it.
[sortedData, indices] = sort(r)
sortedData = 1×10
1 2 3 4 5 6 7 8 9 10
indices = 1×10
5 4 9 1 6 3 7 8 2 10
We can get back to r from sortedData using the indices.
recreatedR(indices) = sortedData
recreatedR = 1×10
4 9 6 2 1 5 7 8 3 10
Let's check.
isequal(r, recreatedR)
ans = logical
1
We could also recreate sortedData from r using indices.
isequal(sortedData, r(indices))
ans = logical
1
  1 Kommentar
Amine Alrharad
Amine Alrharad am 10 Mär. 2023
Hello,
Using arrays it worked, but with matrix is not giving the same order.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by