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 (최근 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
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
2013년 11월 26일
편집: Andrei Bobrov
2013년 11월 26일
[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'];
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!