Merge arrays rows based on similarity in their serial number
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2019년 2월 25일
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'
채택된 답변
Andrei Bobrov
2019년 2월 25일
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
Andrei Bobrov
2019년 2월 26일
@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
2019년 2월 26일
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!