I have here two data sets, one I want to use as my X and the other the Y axis.
For the X, which is my TNF placeholder values below, I want to simply sort the data array from least to greatest. Which I already know how to do. The Y values, TPF, are also shown below and correlate specifically to the respective index of the TNF array.
TNF = [e, a, d, c, b]
TPF = [e, a, d, c, b]
Sorted, it looks like:
TNF = [a, b, c, d, e]
So how to I get the TPF values to sort with the TNF values and not independently from least to greatest?

 채택된 답변

Star Strider
Star Strider 2018년 11월 30일

1 개 추천

Ise the second output of the sort function to return the indices, then use those to map ‘TPF’:
TNF = ['e', 'a', 'd', 'c', 'b']
TPF = ['e', 'a', 'd', 'c', 'b']
[TNFsorted, idx] = sort(TNF);
TPFsorted = TPF(idx)

댓글 수: 4

Jacob Davidson
Jacob Davidson 2018년 12월 1일
편집: Jacob Davidson 2018년 12월 1일
Alright, that made sense when I read it. I tried it, however, and I think I'm going about it wrong. Here's what I have:
[TNFSorted, idx] = sort(TNF);
for i = 1:numel(TPF)
TPFSorted(i) = TPF(idx);
end
I'm also receiving the following error:
Unable to perform assignment because the indices on the left side are not compatible with the
size of the right side.
Error in BMITestScript (line 5)
TPFSorted(i) = TPF(idx)
I'm not super experienced with MATLAB and mainly use the documentation here on Mathworks as a guide, so I don't know how to interpret this error very well.
EDIT:
Actually, I did the following without the loop, I was unaware I didn't need to iterate through the entier array to accomplish this. I put in exactly what your code had in the response and it worked perfectly. Thank you so much!
I appreciate your help!
Star Strider
Star Strider 2018년 12월 1일
As always, my pleasure!
Dav
Dav 2020년 1월 31일
Thanks Star.
Star Strider
Star Strider 2020년 1월 31일
@Dav — My pleasure!

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

추가 답변 (0개)

카테고리

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

태그

질문:

2018년 11월 30일

댓글:

2020년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by