Compare two arrays (intersection) and create new array with output columns, but with repetitions
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi,
I have following example arrays, and I want to write the values of the second array to a new array if the values of the first column intersect.
A = [1 3500; 2 100; 2 564; 3 2140; 3 9820; 2 8952]
B = [1 0 0 0.5; 2 0 0 0.8; 3 0 1 0; 4 0 1 0.5; 5 1 1 0; 6 1 1 0.5]
The output should look like this:
C = [0 0 0.5; 0 0 0.8; 0 0 0.8; 0 1 0; 0 1 0; 0 0 0.8]
I tried to use intersect, but it only returns with no repetition, ismember only returns logical and the position is irrelevant for me. And my workaround code does not rly give me what i like to have:
[~, rowsA, rowsB] = intersect(A(:, 1), B(:, 1));
C = [B(rowsB 2) B(rowsB, 3) B(rowsB, 4)];
% Code with ismember (workaround try)
v = intersect(B(:,1),A(:,1));
bmin = ismember(B(:,1),v(:,1));
bmax = ismember(A:,1),v(:,1));
imin = find(bmin);
imax = find(bmax);
S = B(imin);
T = A(imax);
Is there any workaround that I can use to create this output array?
댓글 수: 0
채택된 답변
per isakson
2019년 11월 18일
편집: per isakson
2019년 11월 18일
Your way of defining A, B and C makes me wonder whether you intended to define matrices, not vectors.
Anyhow, this is a different solution
%%
A = [1 3500, 2 100, 2 564, 3 2140, 3 9820, 2 8952];
B = [1 0 0 0.5, 2 0 0 0.8, 3 0 1 0, 4 0 1 0.5, 5 1 1 0, 6 1 1 0.5];
%% turn the vectors into the matrices I believe you intended to define
A = permute( reshape( A, 2,[] ), [2,1] );
B = permute( reshape( B, 4,[] ), [2,1] );
%%
[~,rowsB] = ismember( A(:,1), B(:,1) );
C = B( rowsB, 2:end );
%% and turn back into a row vector
reshape( permute( C, [2,1] ), 1,[] )
outputs
ans =
0 0 0.5 0 0 0.8 0 0 0.8 0 1 0 0 1 0 0 0 0.8 (edited)
댓글 수: 2
per isakson
2019년 11월 18일
편집: per isakson
2019년 11월 18일
My code assumes a happy path scenario. It assuses that all A(:,1) are members of B(:,1). I missed the if in "to a new array if the values of the first column intersect"
I replaced A by
A = [17 3500, 2 100, 2 564, 3 2140, 3 9820, 2 8952];
and got the same error as you did. 17 is not a member of B(:,1)-
>> Untitled
Index in position 1 is invalid. Array indices must be positive integers or
logical values.
Error in Untitled (line 9)
C = B( rowsB, 2:end );
In this case rowsB(1) is zero.
Proposal: Add the line
rowsB(rowsB==0) = [];
after
[~,rowsB] = ismember( A(:,1), B(:,1) );
추가 답변 (1개)
the cyclist
2019년 11월 17일
It's a little convoluted, but this should work:
A = [1 3500, 2 100, 2 564, 3 2140, 3 9820, 2 8952]
B = [1 0 0 0.5, 2 0 0 0.8, 3 0 1 0, 4 0 1 0.5, 5 1 1 0, 6 1 1 0.5]
[tf,index1] = ismember(A,B(1:4:end));
index2 = 4*index1(tf) - [2 1 0]';
C = B(index2(:))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!