Re-arranging row of a matrix in array.

1 回表示 (過去 30 日間)
Telema Harry
Telema Harry 2021 年 4 月 1 日
コメント済み: Telema Harry 2021 年 4 月 1 日
IHello Programmers,
I am trying to perform the following tasks.
  1. I have a function that returns two vector as output and one of the vector must be arranged in ascending order for further computation.
  2. The second vector would also need to be rearranged such that each element corresponds to the other vector origal element.
[A, B] = Executeprogram(C,D);
% The output of A & B are given below.
% The objective is to arrange A in ascending order such that each element
% of B will still correspond to A
A = 2, 5, -3, 10;
B = 3, 1, 4, 2;
% I want the final answer to be like
% A = -3,2,5,10
% B = 4, 3,1,2
Please how can I perform this tasks in MATLAB?

採用された回答

KSSV
KSSV 2021 年 4 月 1 日
A = [2, 5, -3, 10];
B = [3, 1, 4, 2];
[A,idx] = sort(A)
A = 1×4
-3 2 5 10
idx = 1×4
3 1 2 4
B = B(idx)
B = 1×4
4 3 1 2
  1 件のコメント
Telema Harry
Telema Harry 2021 年 4 月 1 日
Thank you so much.

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2021 年 4 月 1 日
[A,b]=sort(A);
B=B(b);

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by