Merge arrays rows based on similarity in their serial number

1 visualización (últimos 30 días)
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 comentarios
Walter Roberson
Walter Roberson el 25 de Feb. de 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'
Christopher Ibeh
Christopher Ibeh el 25 de Feb. de 2019
Thanks for your question.
Array C contains only rows in A that has a coresponding row in B with the same first element.
-So for the first row in C
We have 12345 in A but no corresponding row in B with the first element as 1, so we have [12345;00000
-for the second row of C
We have 47542 in A but no corresponding row in B with the first element as 1, so we have ...47542, 00000
-For the third row of C
we have 32673 in A and in B the corresponding row in with the fiirst element as 2 is 33425, so we have the second row as ... 32673, 33425
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]

Iniciar sesión para comentar.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 25 de Feb. de 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 comentarios
Andrei Bobrov
Andrei Bobrov el 26 de Feb. de 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
Andrei Bobrov el 26 de Feb. de 2019
Comment by
Thanks a million.
In just three lines. This is perfect!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by