Matlab Merge Array with Offset Values -
say have 2 arrays of double precision in matlab, how can merge them unknown offsets?
for example:
a = 1 1 2 2 3 3 4 4 5 5 b = 2 6 3 5 4 4 5 3 6 2
is there way create these 2 arrays single array 3 columns below when don't know offset/overlap between , b in terms of values in first column?
c = 1 1 nan 2 2 6 3 3 5 4 4 4 5 5 3 6 nan 2
is there efficient way this?
the solution i've come right piece first columns of , b, , proceed use loop iterate through.
a combination of union
unique elements ismember
find corresponding locations trick.
as note, allow a
, b
have number of columns 2 or greater.
a = [1 1;2 2;3 3;4 4;5 5]; b = [2 6;3 5;4 4;5 3;6 2]; %get elements first columns of , b c = union(a(:,1), b(:, 1)); %prepopulate c correct size nan's c = [c, nan(size(c,1), size(a,2) + size(b,2) - 2)]; %find rows of c column 1 of ended [~, i] = ismember(a(:,1), c(:,1)); %stick rest of in rows in first set of free columns c(i, 2:size(a,2)) = a(:,2:end); %now same b in second set of free columns [~, i] = ismember(b(:,1), c(:,1)); c(i, size(a,2) + 1:end) = b(:,2:end); c c = 1 1 nan 2 2 6 3 3 5 4 4 4 5 5 3 6 nan 2
Comments
Post a Comment