matlab - Merge 2nd to last columns for duplicates in 1st column -
i want merge a's 2nd end column if a's 1st column duplicate
from
a = [2 3 1;      3 4 2;      2 6 8] to
b{1}=2 3 1 6   8 b{2}=3 4 2 nan nan or
b = [2 3 1 6    8;      3 4 2 nan nan] row ordering doesn't matter.
my first plan was
a=sortrows(a,1); % sort 1st col and divide various matrices according 1st col (i.e. different matrix different 1st column element)
then horzcat each 2:end elements each matrices.
and join them 1 object in way.
this plan or imagination though can't figure out if possible.
here method started. pad 0 instead of nan though:
b = sortrows(a,1); c = b(1,:);      row = 2:size(b,1)     if b(row,1) == c(end,1)         c(end, end+1:end+2) = b(row, 2:3);     else         c(end+1, 1:3) = b(row, :);     end end or same thing using cell array , not have pad @ all:
b = sortrows(a,1); c = {b(1,:)};      row = 2:size(b,1)     if b(row,1) == c{end}(1)         c{end}(end+1:end+2) = b(row, 2:3);     else         c{end+1} = b(row, :);     end end 
Comments
Post a Comment