Sorting 3 structures together

조회 수: 1 (최근 30일)
Shamsuddeen Abdullahi
Shamsuddeen Abdullahi 2019년 5월 29일
댓글: Stephen23 2019년 5월 29일
Please, I have 3 structures as follows:
x_loc.xx=[3.0, 5.1, 6.5] % x-coordinate
y_loc.yy=[0.5, 1.4, 3.2] % y-coordintates
cash.bb=[14, 1, 6]
I want to sort the structure 'cash' from smallest to biggest. The sorting should also sort the x and coordinates (x_loc.xx and y_loc.yy).
i.e I want the answer to be:
cash.bb=[1,6,14]
x_loc.xx=[5.1, 6.5, 3.0]
y_loc.xx=[1.4, 3.2,0.5].
I coluldnt think of how to start.... pls help
Thanks in anticipation

채택된 답변

Stephen23
Stephen23 2019년 5월 29일
편집: Stephen23 2019년 5월 29일
"I coluldnt think of how to start...."
If you want to sort something, the best place to start is to read the sort documentation. Then you would learn that sort has a second output argument, which are indices that you can use to sort any other arrays that you want:
[cash.bb,idx] = sort(cash.bb)
x_loc.xx = x_loc.xx(idx)
y_loc.yy = y_loc.yy(idx)
Note: having separate structures named x_loc and y_loc does not seem to be a very efficient way to arrange your data.
  댓글 수: 2
Shamsuddeen Abdullahi
Shamsuddeen Abdullahi 2019년 5월 29일
I changed them to the same structure:
cash.bb
x_loc.bb
y_loc.bb
it works.
thanks
Stephen23
Stephen23 2019년 5월 29일
"I changed them to the same structure:"
cash.bb
x_loc.bb
y_loc.bb
Nope. Those are still three separate structures.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by