inserting data values at correct row in a matrix using Matlab

I have 2 matrices with 5 columns but different numbers of rows (up to a couple thousands in actual matrices), e.g.:
A=[1 2 3 4 98
2 1 3 4 51
3 2 4 1 48
4 2 1 3 79];
and
B=[1 2 3 4 87
3 2 4 1 52
4 2 1 3 81];
The 4 first columns represent coordinates and the fifth column is a measured value. I have to collate the data sets by essentially including a sixth column to matrix A and insert the measured values from B at the correct coordinates (missing points should be zeros).
I am trying to do this by creating a new matrix, C, based on A with an extra column (vector) of zeros:
newcolumn = zeros(size(A(:,1)));
C = [A newcolumn];
Until this point everything works fine, but I am a newbie in Matlab and I cannot find a way of actually putting in the measured values from B at the correct coordinates in C and replacing the inserted zero at that specific coordinate.
I've figured a loop going through each row's first four columns could do the job, but cannot seem to find the correct syntax for doing so. Can it be done in this way or am I missing a smarter approach?
Thanks for any help Inooraq

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 11월 18일
C = [A zeros(size(A,1),1)]
a = ismember(A(:,1:end-1),B(:,1:end-1),'rows')
C(a,end) = B(:,end)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2011년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by