Compare two tables arrays of different sizes

4 views (last 30 days)
Alessandro Zappulo
Alessandro Zappulo on 19 Dec 2020
Edited: Ive J on 20 Dec 2020
I have two cell arrays of different sizes "A" is a 96x14 table and "B" is a 95x14 table.
I need to compare the Column 1-2 of "A" with "B", and take only those in common create a "C" using the following math operation:
C= Columns 1-2 = ("01", "Live anim.."), Columns 3-14 = ( A(1,3:end) / B(1,3:end)) only if, as mentioned before, if e.g "01" is in both A and B. So column 3 should be (161896/1596) and so on.
Follow below an example.
As you can see for example "08" is present only in "B" and I don't want to take it in C but I don't know how to set the function.
Thank you!

Answers (1)

Ive J
Ive J on 20 Dec 2020
Edited: Ive J on 20 Dec 2020
% create two tables
[t1, t2] = deal(table("0" + (1:5)', "blah" + (1:5)',...
repmat(1:3, 5, 1), 'VariableNames', {'v1', 'v2', 'v'}));
t2.v1([2, 4]) = "0N";
t1.v2(1) = "blahN";
t2.v = t2.v + 3;
t1 = splitvars(t1, 'v');
t2 = splitvars(t2, 'v');
t1
v1 v2 v_1 v_2 v_3
____ _______ ___ ___ ___
"01" "blahN" 1 2 3
"02" "blah2" 1 2 3
"03" "blah3" 1 2 3
"04" "blah4" 1 2 3
"05" "blah5" 1 2 3
t2
v1 v2 v_1 v_2 v_3
____ _______ ___ ___ ___
"01" "blah1" 4 5 6
"0N" "blah2" 4 5 6
"03" "blah3" 4 5 6
"0N" "blah4" 4 5 6
"05" "blah5" 4 5 6
commonIdx = t1.v1 == t2.v1 & t1.v2 == t2.v2; % common index based on first 2 cols
t1(~commonIdx, :) = []; t2(~commonIdx, :) = []; % keep only rows in common
t1{:, 3:end} = t1{:, 3:end}./t2{:, 3:end};
t1
v1 v2 v_1 v_2 v_3
____ _______ ____ ___ ___
"03" "blah3" 0.25 0.4 0.5
"05" "blah5" 0.25 0.4 0.5
So, t1 is the final merged table.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!