Sorting array and accessing its data
Afficher commentaires plus anciens
I have an array of 10 elements let say X=[4.7955 4.9155 5.1647 5.2406 5.0180 4.9145 4.9905 4.7852 4.8335 5.0465] after sorting it using
[B,I] = sort(X,'ascend') i easily get the index as :
I=[8 1 9 6 2 7 5 10 3 4]
My question is how to access the sorted index data for futher process for example i want to access the data at index 8.
Réponses (2)
Mario Malic
le 24 Jan 2021
Modifié(e) : Mario Malic
le 24 Jan 2021
MATLAB Onramp will get you covered on basics of MATLAB.
To get the value you asked for, use this
X(8)
The vector B holds the sorted data, so if you index into it, like this, you'll get X(8) value
B(1)
4 commentaires
Amin Waqas
le 24 Jan 2021
Mario Malic
le 24 Jan 2021
Then you need X(8). If you are looking for the smallest value in your vector, use
X=[4.7955, 4.9155, 5.1647, 5.2406, 5.0180, 4.9145, 4.9905, 4.7852, 4.8335, 5.0465];
[val, pos] = min(X)
Amin Waqas
le 24 Jan 2021
Mario Malic
le 24 Jan 2021
I still don't understand, this will give you the vector sorted by I indices.
X(I)
Amin Waqas
le 24 Jan 2021
0 votes
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!