Home > other >  How to relate elements back to each other given a new list
How to relate elements back to each other given a new list

Time:09-12

So I have an array of elements, x and y x = [11; 22; 5; 12; 42; 21; 41; 32; 10; ] y = [0; 1; 0; 1; 0; 1; 0; 1; 0; ]

Each element position in array x corresponds to array y elements. For example, 11 corresponds to 0, 22 corresponds to 1, 5 corresponds to 0, etc. I went ahead and created a new array which orders the x array in ascending order:

Now given the new array: c = [5; 10; 11; 12; 21; 22; 32; 41; 42]

I want to now remake the y array so that the array aligns with the new array c, I am having trouble thinking of a way to do that in a way that is not hard coding. Please help.

CodePudding user response:

You want the second output of sort, which tells you the sorting that was applied to its input, so you can apply it to the other array:

[c, ind] = sort(x);
d = y(ind);
  • Related