フィルターのクリア

how convert the vector to matrix ? unknown number of columns

7 ビュー (過去 30 日間)
reta jon
reta jon 2021 年 7 月 29 日
コメント済み: Image Analyst 2021 年 7 月 31 日
Is it possible to convert the vector to matrix .unknown number of columns and the number of row is known. Where is the row filled with zeros? Or with holes?
A=[1 2 3 4 5 6 7 8 9]
number of row=3
A=[1 2 3 4
5 6 7 8
9 0 0 0]
  1 件のコメント
Matt J
Matt J 2021 年 7 月 31 日
編集済み: Matt J 2021 年 7 月 31 日
The task is ambiguous without additional specifications on how the number of columns is to be chosen. For example, with the input,
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
nrows=3;
there are at least three possible 3-row arrangements that would be valid:
B=[1 2 3 4 5
6 7 8 9 10
11 12 13 14 15]
B=[1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 0 0 0]
B=[1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 0 0 0 0 0 0]

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

回答 (2 件)

dpb
dpb 2021 年 7 月 29 日
A=1:9;
nRow-3;
>> A=reshape(A,3,[])
A =
1 4 7
2 5 8
3 6 9
>>
The posted in the Q? text above forced four columns, not three (3) rows...
That, of course, is also doable with a little more effort...
A=1:9;
nCol=4;
nZ=ceil(numel(A)/4);
>> reshape([A zeros(1,nCol*nZ-numel(A))],nCol,[]).'
ans =
1 2 3 4
5 6 7 8
9 0 0 0
>>
  6 件のコメント
Matt J
Matt J 2021 年 7 月 31 日
編集済み: Matt J 2021 年 7 月 31 日
@dpb yes, but augment to what? There isn't a unique choice.For the following input, for example,
A=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
there are at least three possible solutions with 3 rows:
B=[1 2 3 4 5
6 7 8 9 10
11 12 13 14 15]
B=[1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 0 0 0]
B=[1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 0 0 0 0 0 0]
Image Analyst
Image Analyst 2021 年 7 月 31 日
@Matt J, yes there are multiple possible answers. But since he did not actually ask how to construct any of them, and all he asked was "Is it possible to convert the vector to matrix .unknown number of columns and the number of row is known?" the answer is simply "Yes".

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


Matt J
Matt J 2021 年 7 月 29 日
No, the solution is ill-defined. Another result with the same number of rows is:
A=[1 2 3
4 5 6
7 8 9]

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by