Merge arrays rows based on similarity in their serial number
2 views (last 30 days)
Show older comments
Christopher Ibeh
on 24 Feb 2019
Commented: Andrei Bobrov
on 26 Feb 2019
I have two arrays, A & B. The first digit of each row is the serial number.
A = [ 12345;
47542;
32673;
65436;
75343;
23496;
54765]
B = [23566;
33425;
65438;
75354]
How do I combine A and B to have an array C, such that all rows in A with the same serial number in B are concatenated horizontally.
C should be:
C = [12345, 00000;
47542, 00000;
32673, 33425;
65436, 00000;
75343, 75354
23496, 23566;
54765, 00000]
After sorting based on the first row, we have:
C = [12345, 00000;
23496, 23566;
32673, 33425;
47542, 00000;
54765, 00000;
65436, 00000;
75343, 75354]
i tried
y=ismember(A(:,1), B(:,1), 'rows');
t=find(y);
C= [A(t,1:12),B(t,1:12)];
But got an error message
3 Comments
Walter Roberson
on 25 Feb 2019
Instead of "the first column of each row" being the serial number, it appears that the first digit is the serial number. Or perhaps 12345 is intended to represent [1 2 3 4 5] or '12345'
Accepted Answer
Andrei Bobrov
on 25 Feb 2019
A = [ 12345;
47542;
32673;
65436;
75343;
23496;
54765];
B = [23566;
33425;
65438;
75354];
[~,ii] = min(abs(A - B(:)'));
D = zeros(size(A));
D(ii) = B;
C = [A, D];
3 Comments
Andrei Bobrov
on 26 Feb 2019
@Christopher Ibeh
Again my typo - corrected:
A = [3,0.0068,0.046,90,0.00;
4,0.014,0.063,36.64,1.51;
6,0.0063,0.044,90,0.00;
7,0.40,0.20,4.99,58.78;
8,0.017,0.069,0.00,90];
B = [1,0.034,0.072,55.85,-13.00;
2,0.012,0.051,79.95,-114.65;
3,0.14,0.12,20.68,113.77;
4,0.00,0.045,90.00,0.00;
6,0.015,0.054,16.77,107.71;
9,0.44,0.055,33.45,50.37];
ta = array2table(A);
tb = array2table(B);
C = outerjoin(ta,tb,'Keys',1,'Type','left');
Andrei Bobrov
on 26 Feb 2019
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!