inserting data values at correct row in a matrix using Matlab
Ältere Kommentare anzeigen
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
Antworten (1)
Andrei Bobrov
am 18 Nov. 2011
C = [A zeros(size(A,1),1)]
a = ismember(A(:,1:end-1),B(:,1:end-1),'rows')
C(a,end) = B(:,end)
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!