Add data from one matrix to another by the values of one column
Show older comments
I have two matrix of different sizes and they both have two columns, one with ID of the data and other with the data i want. Is there any way to add data with the same ID number from the first column in a third column without using a loop?
The matrix are both cell arrays like these:
1000 [10370;10371;10372] 1002 52
1001 [12933;12934] 2000 60
1002 10001 1000 42
2000 11320 2003 57
2001 [11347;11348]
2002 [10362;10363]
2003 [12632;12633]
And the desired result would be something like this
1000 [10370;10371;10372] 42
1001 [12933;12934]
1002 10001 52
2000 11320 60
2001 [11347;11348]
2002 [10362;10363]
2003 [12632;12633] 57
Accepted Answer
More Answers (1)
C1 = {
1000 [10370;10371;10372]
1001 [12933;12934]
1002 10001
2000 11320
2001 [11347;11348]
2002 [10362;10363]
2003 [12632;12633]};
C2 = {1002 52
2000 60
1000 42
2003 57};
[tf,loc] = ismember([C2{:,1}]',[C1{:,1}]');
C3 =[C1, cell(height(C1),1)];
C3(loc,3) = C2(:,2)
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!