フィルターのクリア

Sort Matrix based on column of another matrix

2 ビュー (過去 30 日間)
Christopher Ibeh
Christopher Ibeh 2019 年 8 月 5 日
コメント済み: Alex Mcaulley 2019 年 8 月 5 日
I have matrices A and B
A=[1 1 4; 2 2 5; 3 3 6; 4 4 8; 5 5 9; 6 8 5; 7 9 4; 8 1 2; 9 3 4; 10 4 1];
B=[3;5;7;2;1;8;4;6;9];
I want to sort matrix A using the values in the column matrix B to have the C below:
C=[A=[3 3 6; 5 5 9; 7 9 4; 2 2 5; 1 1 4; 8 1 2; 4 4 8; 6 8 5; 9 3 4]
I have tried the code below, but it seems not to work
q=ismember(A(:,1), B(:,1), 'rows');
z=find(q);
C=A(z,1:3);

回答 (1 件)

Alex Mcaulley
Alex Mcaulley 2019 年 8 月 5 日
  2 件のコメント
Christopher Ibeh
Christopher Ibeh 2019 年 8 月 5 日
Thanks but am getting 27 rows instead of 9 rows for C.
Alex Mcaulley
Alex Mcaulley 2019 年 8 月 5 日
Sure?
A = [1 1 4; 2 2 5; 3 3 6; 4 4 8; 5 5 9; 6 8 5; 7 9 4; 8 1 2; 9 3 4; 10 4 1];
B = [3;5;7;2;1;8;4;6;9];
C = A(B,:)
C =
3 3 6
5 5 9
7 9 4
2 2 5
1 1 4
8 1 2
4 4 8
6 8 5
9 3 4

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by