How to conditionally pick the rows of tow datasets?

2 ビュー (過去 30 日間)
Andi
Andi 2022 年 7 月 20 日
コメント済み: Andi 2022 年 7 月 21 日
Hi everyone,
I have two data set and want to pick rows that have common first element. In the below example, when first element of matrix A (row) is same with the first element of matrix B (row) the second element of B added to matrix A.
Thank you!
A = [1, 6, 3;
8, 11, 1;
4, 2, 0;
9, 2, 1]
B = [3, 11;
3, 1;
8, 4;
2, 5;
1, 8;
9, 7;
4, 6;
0, 1;
11, 25;
6, 15]
%% Dimesnions of A and B are not same
% then C
C = [1, 6, 3, 8;
8, 11, 1, 4;
4, 2, 0, 6;
9, 2, 1, 7]
% D be another empty arry that store the upper and lower values of second
% coloumn (B)
D=[4, 5, 8, 7, 6;
11, 1, 4, 5, 8;
8, 7, 6, 1, 25;
5, 8, 7, 6, 1]
% need to repeat this process for 5000 rows of column A, where the length
% of B is 17000
Here is the proposed methods alongwith the real data set, but none of them working on real data.
A=readmatrix('data_1.csv');
B=readmatrix('data_2.csv');
% Approach 1
B = array2table(B)
[ism,idx] = ismember(A(:,1),B(:,1));
C(ism,end+1) = B(idx(ism),2)
% Approach 2
C = outerjoin(A,B,'Key','Var1','Type','left')
The data is also attached here.

採用された回答

Voss
Voss 2022 年 7 月 20 日
A = [1, 6, 3; 8, 11, 1; 4, 2, 0; 8, 1, 5; 9, 2, 1]
A = 5×3
1 6 3 8 11 1 4 2 0 8 1 5 9 2 1
B = [8, 4; 7, 1; 2, 5; 1, 8; 9, 7; 4, 6]
B = 6×2
8 4 7 1 2 5 1 8 9 7 4 6
C = A;
[ism,idx] = ismember(A(:,1),B(:,1));
C(ism,end+1) = B(idx(ism),2)
C = 5×4
1 6 3 8 8 11 1 4 4 2 0 6 8 1 5 4 9 2 1 7
  7 件のコメント
Voss
Voss 2022 年 7 月 21 日
Can you give an example?
Andi
Andi 2022 年 7 月 21 日
Thanks, I already fixed that issue.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by