Can you help me ?

1 回表示 (過去 30 日間)
zainab hp
zainab hp 2015 年 11 月 5 日
コメント済み: Walter Roberson 2015 年 11 月 7 日
Salam: I want to combine two or more row with same first column in the matrix .
from
A = [1 2 3 4;
1 5 6 1;
2 2 3 4;
2 5 6 1;
2 6 7 8;
3 1 2 3;
4 1 2 3
4 5 8 7];
to
A = [1 2 3 4 5 6 1 0 0 0;
2 2 3 4 5 6 1 6 7 8;
3 6 7 8 0 0 0 0 0 0;
4 1 2 3 5 8 7 0 0 0];
  2 件のコメント
Image Analyst
Image Analyst 2015 年 11 月 5 日
Why?
What kind of array do you want? A double with NaN in the "missing" columns on the right, or a cell array with nulls in the missing cells on the right?
zainab hp
zainab hp 2015 年 11 月 7 日
編集済み: zainab hp 2015 年 11 月 7 日
Salam: I want to combine two or more row with same first column in the matrix .
from
A = [1 2 3 4;
1 5 6 1;
2 2 3 4;
2 5 6 1;
2 6 7 8;
3 1 2 3;
4 1 2 3
4 5 8 7];
to
A = [1 2 3 4 5 6 1 0 0 0;
2 2 3 4 5 6 1 6 7 8;
3 6 7 8 0 0 0 0 0 0;
4 1 2 3 5 8 7 0 0 0];

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 6 日
Numeric arrays cannot have different number of columns for each row.
  2 件のコメント
zainab hp
zainab hp 2015 年 11 月 7 日
編集済み: zainab hp 2015 年 11 月 7 日
Salam: I want to combine two or more row with same first column in the matrix .
from
A = [1 2 3 4;
1 5 6 1;
2 2 3 4;
2 5 6 1;
2 6 7 8;
3 1 2 3;
4 1 2 3
4 5 8 7];
to
A = [1 2 3 4 5 6 1 0 0 0;
2 2 3 4 5 6 1 6 7 8;
3 6 7 8 0 0 0 0 0 0;
4 1 2 3 5 8 7 0 0 0];
Walter Roberson
Walter Roberson 2015 年 11 月 7 日
A1 = A(:,1);
[uA1, ~, idx] = unique(A1);
maxidx = max(idx);
maxmerge = max( histc(idx, 1:maxidx) );
Anew = zeros(maxidx, size(A,2)*(maxmerge - 1) + 1);
Anew(:,1) = uA1;
for K = 1 : length(idx)
targetrow = idx(K);
now put A(K,2:end) at the "end" of Anew(targetrow,:)
end
Now to put A(K,2:end) at the "end" of Anew(targetrow,:) is left as an exercise for you.
Note: you did not define the order of the row results when column 1 of A is not in sorted order. I picked an arbitrary order that aesthetically pleased me.
Note: you did not define the order of merging the rows that have the same column 1. I picked an arbitrary order that aesthetically pleased me.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by