How to add a matrix A to another matrix B with a different size and it should maintain rows of B which do not have the first two elements equal to the first two elements of a row in matrix A?

1 次查看(过去 30 天)
I got the following problem. if I have the following two matrices:
A=[1 2 1
3 4 1
7 8 1]
B=[1 2 0
2 6 0
3 4 0
4 9 0
5 2 0
6 1 0
7 8 0]
I need to add matrix A to matrix B with remaining the rows of matrix B. Only when the first two elements are the same, the row should be replaced. I need a matrix:
C=[1 2 1
2 6 0
3 4 1
4 9 0
5 2 0
6 1 0
7 8 1]
It has to work for a 41120x3 matrix.

回答(3 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-6-11
C=B
C(A(:,1),:)=A
  3 个评论
Glenn Roumen
Glenn Roumen 2013-6-11
A=[0,00700000000000000 0,0125000000000000 22,9700602142857
0,00700000000000000 0,0130000000000000 45,2744142857143
0,00700000000000000 0,0135000000000000 45,1526000000000
0,00700000000000000 0,0145000000000000 11,6694428571429
0,00700000000000000 0,0150000000000000 -4,18851235849057
0,00700000000000000 0,0155000000000000 14,7274881516588
0,00700000000000000 0,0160000000000000 17,6441212592593
0,00700000000000000 0,0165000000000000 15,7269891228070
0,00700000000000000 0,0170000000000000 15,9662873033708
0,00700000000000000 0,0175000000000000 15,0895273170732
0,00700000000000000 0,0180000000000000 4,13977722222222
0,00700000000000000 0,0185000000000000 4,71143543046358
0,00700000000000000 0,0190000000000000 7,75264824561404
0,00700000000000000 0,0195000000000000 14,3002651162791
0,00700000000000000 0,0200000000000000 9,83357122807018
0,00700000000000000 0,0205000000000000 4,21547104347826
0,00700000000000000 0,0210000000000000 2,89294185294118
0,00700000000000000 0,0215000000000000 -0,361855715053765
0,00700000000000000 0,0220000000000000 6,84516331658291
0,00700000000000000 0,0225000000000000 -5,80106941176471
0,00700000000000000 0,0230000000000000 -6,65370203754266
0,00700000000000000 0,0235000000000000 2,20747630844156
0,00700000000000000 0,0240000000000000 -0,625140693693694
0,00700000000000000 0,0245000000000000 -0,868563960869565
0,00700000000000000 0,0250000000000000 1,45180875536480
0,00700000000000000 0,0255000000000000 24,8010066806723
0,00700000000000000 0,0260000000000000 19,0356486307692
0,00700000000000000 0,0265000000000000 -2,00504505479452
0,00700000000000000 0,0270000000000000 -7,92422984575836
0,00700000000000000 0,0275000000000000 4,93771741935484
0,00700000000000000 0,0280000000000000 11,6721689029536
0,00700000000000000 0,0285000000000000 2,40859311608623
0,00700000000000000 0,0290000000000000 7,71062057932693
0,00700000000000000 0,0295000000000000 11,5156263656250
0,00700000000000000 0,0300000000000000 6,85182836776860
0,00700000000000000 0,0305000000000000 0,829217855867343
0,00700000000000000 0,0310000000000000 3,43887444564047
0,00700000000000000 0,0315000000000000 1,33481978260870
0,00700000000000000 0,0320000000000000 0,969204675742574
0,00700000000000000 0,0325000000000000 11,7114356995582
0,00700000000000000 0,0330000000000000 18,3358246666667
0,00700000000000000 0,0335000000000000 8,59924499402985
0,00700000000000000 0,0340000000000000 4,31928491043398
0,00700000000000000 0,0345000000000000 1,19245761369672
0,00700000000000000 0,0350000000000000 5,29711275917431
0,00700000000000000 0,0355000000000000 -0,818585976009945
0,00700000000000000 0,0360000000000000 2,07462945737328
0,00700000000000000 0,0365000000000000 -1,17016255909850
0,00700000000000000 0,0370000000000000 8,66623280105634
0,00700000000000000 0,0375000000000000 2,68851779796265
0,00700000000000000 0,0380000000000000 1,30456810066143
0,00700000000000000 0,0385000000000000 -2,50241116263254
0,00700000000000000 0,0390000000000000 -4,53419951812161
0,00700000000000000 0,0395000000000000 -0,318344505146104]
B=[0,00700000000000000 0,000500000000000000 0
0,00700000000000000 0,00100000000000000 0
0,00700000000000000 0,00150000000000000 0
0,00700000000000000 0,00200000000000000 0
0,00700000000000000 0,00250000000000000 0
0,00700000000000000 0,00300000000000000 0
0,00700000000000000 0,00350000000000000 0
0,00700000000000000 0,00400000000000000 0
0,00700000000000000 0,00450000000000000 0
0,00700000000000000 0,00500000000000000 0
0,00700000000000000 0,00550000000000000 0
0,00700000000000000 0,00600000000000000 0
0,00700000000000000 0,00650000000000000 0
0,00700000000000000 0,00700000000000000 0
0,00700000000000000 0,00750000000000000 0
0,00700000000000000 0,00800000000000000 0
0,00700000000000000 0,00850000000000000 0
0,00700000000000000 0,00900000000000000 0
0,00700000000000000 0,00950000000000000 0
0,00700000000000000 0,0100000000000000 0
0,00700000000000000 0,0105000000000000 0
0,00700000000000000 0,0110000000000000 0
0,00700000000000000 0,0115000000000000 0
0,00700000000000000 0,0120000000000000 0
0,00700000000000000 0,0125000000000000 0
0,00700000000000000 0,0130000000000000 0
0,00700000000000000 0,0135000000000000 0
0,00700000000000000 0,0140000000000000 0
0,00700000000000000 0,0145000000000000 0
0,00700000000000000 0,0150000000000000 0
0,00700000000000000 0,0155000000000000 0
0,00700000000000000 0,0160000000000000 0
0,00700000000000000 0,0165000000000000 0
0,00700000000000000 0,0170000000000000 0
0,00700000000000000 0,0175000000000000 0
0,00700000000000000 0,0180000000000000 0
0,00700000000000000 0,0185000000000000 0
0,00700000000000000 0,0190000000000000 0
0,00700000000000000 0,0195000000000000 0
0,00700000000000000 0,0200000000000000 0
0,00700000000000000 0,0205000000000000 0
0,00700000000000000 0,0210000000000000 0
0,00700000000000000 0,0215000000000000 0
0,00700000000000000 0,0220000000000000 0
0,00700000000000000 0,0225000000000000 0
0,00700000000000000 0,0230000000000000 0
0,00700000000000000 0,0235000000000000 0
0,00700000000000000 0,0240000000000000 0
0,00700000000000000 0,0245000000000000 0
0,00700000000000000 0,0250000000000000 0
0,00700000000000000 0,0255000000000000 0
0,00700000000000000 0,0260000000000000 0
0,00700000000000000 0,0265000000000000 0
0,00700000000000000 0,0270000000000000 0
0,00700000000000000 0,0275000000000000 0
0,00700000000000000 0,0280000000000000 0
0,00700000000000000 0,0285000000000000 0
0,00700000000000000 0,0290000000000000 0
0,00700000000000000 0,0295000000000000 0
0,00700000000000000 0,0300000000000000 0
0,00700000000000000 0,0305000000000000 0
0,00700000000000000 0,0310000000000000 0
0,00700000000000000 0,0315000000000000 0
0,00700000000000000 0,0320000000000000 0
0,00700000000000000 0,0325000000000000 0
0,00700000000000000 0,0330000000000000 0
0,00700000000000000 0,0335000000000000 0
0,00700000000000000 0,0340000000000000 0
0,00700000000000000 0,0345000000000000 0
0,00700000000000000 0,0350000000000000 0
0,00700000000000000 0,0355000000000000 0
0,00700000000000000 0,0360000000000000 0
0,00700000000000000 0,0365000000000000 0
0,00700000000000000 0,0370000000000000 0
0,00700000000000000 0,0375000000000000 0
0,00700000000000000 0,0380000000000000 0
0,00700000000000000 0,0385000000000000 0
0,00700000000000000 0,0390000000000000 0
0,00700000000000000 0,0395000000000000 0
0,00700000000000000 0,0400000000000000 0]

请先登录,再进行评论。


Roger Stafford
Roger Stafford 2013-6-11
[tf,loc] = ismember(B(:,1:2),A(:,1:2),'rows');
t = loc(tf);
C = B;
C(t,:) = A(t,:);
Note that if B contains more than one row with the same first two elements, this will select the one with the higher index.
  5 个评论
Roger Stafford
Roger Stafford 2013-6-11
A thousand pardons! My own code is in error. I should have written this:
[tf,loc] = ismember(B(:,1:2),A(:,1:2),'rows');
C = B;
C(tf,3:end) = A(loc(tf),3:end);
Roger Stafford
Roger Stafford 2013-6-11
If I understand you correctly, the reason you are getting a 41120 x 5 array is that your system is NOT handling commas as you expect! It is interpreting them is dividing symbols between separate elements of a five-element row. Try this simple experiment on your machine:
x = [1,2 3,4]
and see how many elements x has and what they are. If there are two and they are fractional, your system is dealing with commas properly. If there are four and they are all integers, it is not using commas properly.

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2013-6-11
for k=1:size(B,1)
ii=find(ismember(A(:,1:2),B(k,1:2),'rows'),1);
if ~isempty(ii)
B(k,:)=A(ii,:);
end
end

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by