0

I was using "intersect" in my Matlab code to do the sorting where I want the following:

[ch] = sort(s, 'ascend'); 

[same, a] = intersect(s, ch); 

For example:

input:

s = [55 21 78 7]

output:

ch = [7 21 55 78]
a = [4 2 1 3]

I need to access a where a shows the original index prior to sorting so I can use it for further processing.

This method works exactly as what I want, but I guess it is taking a lot of time to do the sorting and intersect etc especially when the size of s approaching 100 or higher, are there faster or smarter ways to do so?

Thank you very much.

marsei
  • 7,691
  • 3
  • 32
  • 41
user2563812
  • 219
  • 1
  • 2
  • 12

1 Answers1

1

You can achieve this with

[ch IX] = sort(s, 'ascend') 

where IX is identical to a.

marsei
  • 7,691
  • 3
  • 32
  • 41