MATLAB: Sort one set of data to correspond to another.

sort

Say I have an ordered set of data.
a = [100,200,300,400,500];
And say I have another set of data,
b = [300,200,500,400,100]
I m trying to find the index where a is sorted to b. I could use a nested for loop of course but is there a better way to get the index shown below?
c = [5,2,1,4,3]

Best Answer

If b is an unsorted version of a, i.e., all elements in b occur once and only once in a, you can use
[~, idx] = sort(b);
In the more general case where b can have some elements of a, and elements can occur more than once, use
for i=1:numel(b), idx(i) = find(ismember(a, b(i))); end
Instead of the for loop, you can also use
idx = arrayfun(@(x) find(ismember(a,x)), b);