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]

3 次查看(过去 30 天)
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 个评论
dpb
dpb 2013-11-26
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.

请先登录,再进行评论。

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2013-11-26
编辑:Andrei Bobrov 2013-11-26
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'];

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by