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?

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.

Answers (3)

C=B
C(A(:,1),:)=A

3 Comments

It works for this case. I have to be more specific. For my actual case I don't have integers and than this won't work.
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]

Sign in to comment.

[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 Comments

Thank you for your answer. However this still does not work for my actual case. My actual A matrix has size (1213x3) and matrix has size (41120x3). I don't know why. I also don't have integers.
In what way does it not work? Does it give an error message or does it finish execution but simply make the wrong substitutions? In the latter case, can you provide a simple example with only a few elements in which its substitutions are in error?
Looking at your most recent example I notice that either your arrays have five columns or you are using commas for decimal points. Does your system properly handle commas in that way?
Also with floating point decimal fractions there is always the problem of numbers that look equal in their display but are actually different down in their least significant bits due to differing rounding procedures in producing them. The 'ismember' function demands exact equality.
I copied this from a data file. It handles commas well. And the rounding is no problem. At first it gives me a 41120x5 matrix and it should be a 41120x3 matrix (the last two rows are filled with zeros). Secondly the rows are not sorted properly. Like this part of the matrix:
C=[0,00500000000000000 0,0195000000000000 9,73521395111110
0,00500000000000000 0,0200000000000000 -0,0290952789473685
0,00500000000000000 0,0205000000000000 7,47232483699059
0,00500000000000000 0,0210000000000000 9,44959263392857
0,00400000000000000 0,00400000000000000 -Inf
0,00500000000000000 0,0220000000000000 4,35354656578947
0,00500000000000000 0,0225000000000000 31,5868570714285
0,00500000000000000 0,0230000000000000 33,7034023227513
0,00500000000000000 0,0235000000000000 -4,53910826979471
0,00500000000000000 0,0240000000000000 -9,87124479166666
0,00500000000000000 0,0245000000000000 -6,91306111111111
0,00500000000000000 0,0250000000000000 4,98642755555556
0,00400000000000000 0,00800000000000000 -Inf
0,00400000000000000 0,00850000000000000 -Inf
0,00400000000000000 0,00900000000000000 -Inf
0,00500000000000000 0,0270000000000000 4,11512575806452
0,00500000000000000 0,0275000000000000 2,68597533396625
0,00500000000000000 0,0280000000000000 9,02200131021194]
When I use sortrows it gives crazy results.
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);
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.

Sign in to comment.

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

1 Comment

Thank you for your answer. However it still does not work. I don't know why but a part of the matrix is set to zero.

Sign in to comment.

Asked:

on 11 Jun 2013

Community Treasure Hunt

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

Start Hunting!