MATLAB: Insert rows from one matrix into another that share the same column numbers

MATLABmatrix array

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?

Best Answer

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