Efficient method for populating a matrix based on its equality with another matrix
Show older comments
Hello. I'm having a problem populating a matrix and would greatly appreciate some help.
If we have:
if true
A = 10 11 3 4 B = 10 11 NaN NaN
12 11 4 6 12 11 NaN NaN
13 13 2 8 13 11 NaN NaN
end
What I want is the following: if A(i,1:2) and B(i,1:2) are the same, then B(i,3:4) = A(i,3:4). So something like this:
if true
B = 10 11 3 4
12 11 4 6
13 11 NaN NaN
end
I've managed to create a loop that achieves this result. Which is the following:
if true
for i = 1:length(B);
for j = 1:length(A);
if B(i,1) == A(j,1) && B(i,2) == A(j,2);
B(i,3) = A(j,3);
end
end
end
end
The problem is, the two arrays I'm working with have close to 3 million rows so a double conditioned loop is extremely slow. Is there a vectorised way to achieve the same result that is significantly quicker?
Any help would be greatly appreciated.
Thanks
Andrew
Accepted Answer
More Answers (0)
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!