フィルターのクリア

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 日間)
yousef Yousef
yousef Yousef 2013 年 11 月 26 日
編集済み: Andrei Bobrov 2013 年 11 月 26 日
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 Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by