Index and sort through vector by only picking the values that are the same
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Tyler Ram
il 8 Nov 2018
Commentato: Star Strider
il 8 Nov 2018
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 Commenti
Risposta accettata
Star Strider
il 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 Commenti
Star Strider
il 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!