Insert rows from one matrix into another that share the same column numbers?
1 view (last 30 days)
Show older comments
Matthew Arenas
on 18 Feb 2019
Commented: Matthew Arenas
on 19 Feb 2019
I have a matrix that looks like this
M = [0 2011 1 0 0
0 2011 1 1 0
0 2011 1 2 0
0 2011 1 3 0]
and one that looks like this
N = [1 2011 1 0 399.15
1 2011 1 1 399.93
1 2011 1 2 399.52
1 2011 1 3 399.59]
How do insert data from N into M if it matches the numbers in columns 2 through 4?
0 Comments
Accepted Answer
Walter Roberson
on 18 Feb 2019
[found, idx] = ismember(N(:,2:4), M(:,2:4), 'rows');
M(idx(found), 5) = N(found,5);
However the code in this form will not work for the situtation where there are multiple matches (on either side), in which case you would need to clarify what "insert" meant. If there were multiple sources, should they be totaled, or averaged, or the maximum taken, or ?
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!