how to predefine a matrix with one unknown dimension

137 ビュー (過去 30 日間)
Eliraz Nahum
Eliraz Nahum 2018 年 10 月 12 日
移動済み: Dyuman Joshi 2023 年 10 月 18 日
hey everybody I am interested in predefining a matrix with unknown dimension. I know that the matrix should have 15 rows (number of iterations), but the number of columns (time stages) depends on a stop condition in the code, and hence can't be known in advance. can I create a general definition like v=[]; but for a matrix with one known dimension? In addition, is it possible to create a zero matrix with 1 unknown dimension?
thanks!
  1 件のコメント
Eliraz Nahum
Eliraz Nahum 2018 年 10 月 13 日
移動済み: Dyuman Joshi 2023 年 10 月 18 日
Thank you all. Both ways are good and I even learned some new elegant things.

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

採用された回答

Matt J
Matt J 2018 年 10 月 12 日
編集済み: Matt J 2018 年 10 月 12 日
No, all the dimensions of a matrix must be explicitly defined. What I often do in those cases is pre-define a cell array,
C=cell(1,N)
with a conservatively large N. Then, as the loop runs, accumulate the data in a sequence of cells,
for i=1:N
C{i}=next_column;
if stop_condition
break;
end
end
Later, you can just assemble the cells into a matrix with,
A=cell2mat(C);
and the empty cells will be ignored.

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 10 月 13 日
Just define a matrix that's well large enough to hold all your numbers, like a few million elements or whatever.
v = zeros(15, 1000000);
Then do whatever you need to do assigning rows and columns in v, then, if you've kept track of the max column number, you can simply crop the array
v = v(:, maxColumn);

カテゴリ

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

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by