Hi,
I have two vectors for example:
A=[0 8 2 9 1 10 23]
B=[2 30 23 90 45 47 5]
I need to make chart where B is depending of A ( B=f(A) )
I sorted A vector using:
Asorted = sort(A,'ascend')
How to sort B vector to the elements corresponding to the values of A (before sorting)?
In the end i want to get vectors
Asorted=[0 1 2 8 9 10 23]
Bsorted=[2 45 23 30 47 5]

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 29일
편집: Ameer Hamza 2020년 11월 29일

0 개 추천

Second outout if sort() gives the index used to sort the vector. You can use it to arrange element of B.
A=[0 8 2 9 1 10 23];
B=[2 30 23 90 45 47 5];
[Asorted, idx] = sort(A,'ascend')
Bsorted = B(idx)

댓글 수: 4

Aleksandra Pawlak
Aleksandra Pawlak 2020년 11월 29일
It works in code where I define only this 2 vectors, but not works in my code.
In command window i have information:
Unrecognized function or variable 'idx'.
Error in stobc (line 132)
Bsorted=B(idx)
Ameer Hamza
Ameer Hamza 2020년 11월 29일
Can you show your code. How have you used my lines in your code?
Aleksandra Pawlak
Aleksandra Pawlak 2020년 11월 29일
I see the problem- i make missprint: ixd instead of idx in [Asorted, idx].
Thank you and sorry for the confusion
Ameer Hamza
Ameer Hamza 2020년 11월 29일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

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

질문:

2020년 11월 29일

댓글:

2020년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by