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 visualizzazioni (ultimi 30 giorni)
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 Commento
dpb
dpb il 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.

Accedi per commentare.

Risposte (1)

Andrei Bobrov
Andrei Bobrov il 26 Nov 2013
Modificato: Andrei Bobrov il 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'];

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by