I have them matrix a=[2 5;4 6;5 1; 5 2; 5 7;10 3;10 4],I want it to be a=[2 5 0 0;4 6 0 0;5 1 2 7;10 3 4 0]

2 views (last 30 days)
I have them matrix a=[2 5;4 6;5 1; 5 2; 5 7;10 3;10 4],I want it to be a=[2 5 0 0;4 6 0 0;5 1 2 7;10 3 4 0]
  1 Comment
dpb
dpb on 26 Nov 2013
Unless you can define the rule for why about all you can do is create it as you wish.
Otherwise, use the rule for how to decide which rows need augmenting w/ zeros and which need to be smooshed together from following columns and write the code to do it...the first rule appears to be "create a row in new matrix from first two rows of existing by zero-filling to four columns".
After that, it it seems to to "create remaining rows by placing 2nd column of n+1:n+2 rows in 3rd and 4th columns of nth row, with following rows being filled from subsequent columns as long as sufficient data exist, zero-filling from there to make up the last column if necessary."
That's writable in code albeit somewhat messy, it is an implementable algorithm.

Sign in to comment.

Answers (1)

Andrei Bobrov
Andrei Bobrov on 26 Nov 2013
Edited: Andrei Bobrov on 26 Nov 2013
see your question and:
[a1,~,ii] = unique(a,'stable');
n = max(histc(ii,1:ii(end)));
f = @(x){[x(:)' zeros(1,n-numel(x))]};
out = [a1,cell2mat(accumarray(ii,a(:,2),[],f))];
or
[a1,~,ii] = unique(a(:,1),'stable');
i2 = diff([find([true;diff(ii)~=0]);numel(ii)+1]);
m = max(i2);
n = numel(i2);
z=zeros(m,n);
z(m*(0:n-1)'+i2)=1;
z(flipud(cumsum(flipud(z)))>0)=a(:,2);
out = [a1, z'];

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!