Store vector data of not fixed length into a Matrix

4 ビュー (過去 30 日間)
Maximilian Arpaio
Maximilian Arpaio 2018 年 9 月 17 日
編集済み: Maximilian Arpaio 2018 年 9 月 17 日
Dear community, I am trying to store different vectors 1 x N inside a Matrix M x N. The issue in doing this is that the vectors have not the same length N (which is variable for each row) and thus each row M does not have the same number of columns N. I've tried to allocate a zero matrix in advance so that deltas could have been filled in by zeros, but it is not working. How can I overcome this ?
example: A=zeros (8) A(3)=[1 2 3 4] A(1)=[2 6 7 9] A(2)=[4 6 6 7 8 9 1 3]
A(3) and A(1) assignments are returning me an error while A(2) assignment not. For those arrays < 8 elements, I was hoping to get 0s as fill ups to comply to the matrix's length.
Please advise...
  1 件のコメント
John D'Errico
John D'Errico 2018 年 9 月 17 日
No need to post twice.

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

採用された回答

John D'Errico
John D'Errico 2018 年 9 月 17 日
編集済み: John D'Errico 2018 年 9 月 17 日
You cannot store entire vectors into a double matrix as you tried to do. So this is impossible:
A(1)=[2 6 7 9]
A(2)=[4 6 6 7 8 9 1 3]
First, a flat (double precision) array cannot store multiple elements into a single element of that array. Second, an array cannot have rows (or columns) of different lengths.
You must either use a cell or struct array, or store the vectors properly inside a double array. So you COULD have done this:
A(1,1:4)=[2 6 7 9]
but then you are forced to know how long the vector is in advance.
A cell array is fully a better choice.
  1 件のコメント
Maximilian Arpaio
Maximilian Arpaio 2018 年 9 月 17 日
The hint about the cell array is something I was not thinking about ! Thanks a lot

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by