Sorting array and accessing its data

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.

답변 (2개)

Mario Malic
Mario Malic 2021년 1월 24일
편집: Mario Malic 2021년 1월 24일

1 개 추천

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

Amin Waqas
Amin Waqas 2021년 1월 24일
Dear Mario Malic
By doing I(8) i am getting 10 which is the value of I at index 8 but with respect to my question it should be 4.7852
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
Amin Waqas 2021년 1월 24일
This will give me only one value but my requirements is first to sort the values in ascending order than based on their indexing accessing their data corresponding data just like first use the data at index 8 which is 4.7852 then use the next index value data and so on
I still don't understand, this will give you the vector sorted by I indices.
X(I)

댓글을 달려면 로그인하십시오.

Amin Waqas
Amin Waqas 2021년 1월 24일

0 개 추천

Mario Malic might be i am failed to understand you my problem actually, the problem is to first sort the array then after sorting use its indexies to accessing their data just like first access the data at first sort index 8 use it then access the next sorted index and use its data so on. On your suggested solution B(1) i can definately access the data but the problem is not accessing the data using their indexies.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2021년 1월 24일

댓글:

2021년 1월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by