How to join two matrices based on similar array in each

3 ビュー (過去 30 日間)
Amy Xu
Amy Xu 2017 年 4 月 16 日
編集済み: the cyclist 2017 年 4 月 16 日
Matrix A as follows:
A = [
1 35 24 234 243
2 34 234 2 234
2 34 234 2 234
3 34 234 234 12
4 32 324 243 85
4 32 324 243 85
4 32 324 243 85
5 43 234 24 56
5 43 234 24 56
];
Matrix B as follows:
B = [43 65 1
45 546 5
43 6456 2
34 534 3
53 46 4
];
Based on the similar array in column 3 of matrix b and column 1 in matrix A, I want to joint these to matrix.
C = [
43 65 1 35 24 234 243
45 546 5 34 234 2 234
43 6456 2 34 234 2 234
34 534 3 34 234 234 12
53 46 4 32 324 243 85
];

採用された回答

the cyclist
the cyclist 2017 年 4 月 16 日
編集済み: the cyclist 2017 年 4 月 16 日
Here is one way:
uniqueA = unique(A,'rows');
[~,idx] = ismember(B(:,3),uniqueA(:,1));
C = [B,uniqueA(idx,2:end)];
I am making a couple assumptions here, based on your one example:
  • The rows in A with the same values in the first column are also identical throughout.
  • Every value in B(:,3) is represented in A(:,1)

その他の回答 (1 件)

Image Analyst
Image Analyst 2017 年 4 月 16 日
I'm not really sure of your rule about "similar", but this seems to give you the output you gave:
A = [
1 35 24 234 243
2 34 234 2 234
2 34 234 2 234
3 34 234 234 12
4 32 324 243 85
4 32 324 243 85
4 32 324 243 85
5 43 234 24 56
5 43 234 24 56
]
[rowsA, columnsA] = size(A)
% Matrix B as follows:
B = [43 65 1
45 546 5
43 6456 2
34 534 3
53 46 4
]
[rowsB, columnsB] = size(B)
C = A(1:rowsB, 2:end); % Initialize.
C = [B C]

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by