Index and sort through vector by only picking the values that are the same

5 views (last 30 days)
H = [500 500 500 450 450 450 400 400 350 350 350 300 300 300];
I = [-0.0019 -0.0018 -0.0017 -0.0019 -0.0018 -0.0017 -0.0019 -0.0018 -0.0017]
V = [-7.54E-06 -7.23E-06 -6.93E-06 -7.53E-06 -7.21E-06 -6.89E-06 -6.60E-06 -7.50E-06 -7.23E-06 -6.90E-06]
Need to sort through the data and get an IV for each value but lump then off the uniqueness of their value, e.g., I want all the IVs for the H at 400, etc. I know there is a unique function in Matlab, but that only gives me a single element of the array that is unique. Is there a way to loop through the unique values to get the IV values?
Thanks,
Tyler
  5 Comments
Tyler Ram
Tyler Ram on 8 Nov 2018
I attached a more full version, I am taking the data from an excel sheet, hopefully you can see this, the last one got flagged as spam

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 8 Nov 2018
The unique funciton can have 3 outputs, although you have to specifically request them. The third output is the index of each repeated unique value in the vector. You can use those to map into ‘I’ and ‘V’:
H = [500 500 500 450 450 450 400 400 350 350 350 300 300 300];
[Hu,~,idx] = unique(H, 'stable')
The 'stable' argument simply retains the original order, rather than sorting the output.
  2 Comments
Star Strider
Star Strider on 8 Nov 2018
If you want to sort the result, just remove the 'stable' argument. The results and the associated ‘idx’ vector will then be sorted in ascending order.
If you want them sorted in descending order, it would be easiest to use the sort function to do that first, then use unique with the 'stable' argument.

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!