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

array manipulationindexingsplit array

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

Best Answer

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.