when using intersect function i need all rows of B and matching rows of A. how can i do it?

조회 수: 1 (최근 30일)
i have two varibales A and B of variable sizes for each iteration. lets take first iteration. in 1st iteraton i want to find all rows & column values of B and rows of A which matches with B. and for rows which are not matching i want as zero as given below. like 8542 is not present in A but present in B so in old id column for 8542 i want as zero.
eg.
A id B id
2451 10 2451 15
2554 5 2554 7
5419 2 5419 5
8542 9
i want output like this
B id old id
2451 15 10
2554 7 5
5419 5 2
8542 9 0

채택된 답변

Stephen23
Stephen23 2021년 11월 22일
T1 = array2table([2451,10;2554,5;5419,2],'VariableNames',{'A','id'})
T1 = 3×2 table
A id ____ __ 2451 10 2554 5 5419 2
T2 = array2table([2451,15;2554,7;5419,5;8542,9],'VariableNames',{'B','id'})
T2 = 4×2 table
B id ____ __ 2451 15 2554 7 5419 5 8542 9
[X,Y] = ismember(T2.B,T1.A);
Y(X) = T1.id(Y(X));
T2.old_id = Y
T2 = 4×3 table
B id old_id ____ __ ______ 2451 15 10 2554 7 5 5419 5 2 8542 9 0

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by