フィルターのクリア

array that grows in size over time

2 ビュー (過去 30 日間)
vsee
vsee 2011 年 8 月 31 日
Is there an option to declare an array in matlab that grows as a program runs (like a vector or a list in C++)? I am trying to read a .csv file with many data points and check for each data point to meet a certain requirement and if it does initialize it to another array and keep doing so until I reach the end of the file that I read using the csvread command?Thanks for any suggestions.

回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 8 月 31 日
You don't need to declare it. MATLAB will automatically grow it. Although many will advise you not to do that because it slows things done, by in your case, it is really needed. For example,
>> clear a;
a=rand(3,1);
a(5)=20
a =
0.1576
0.9706
0.9572
0
20.0000
  2 件のコメント
vsee
vsee 2011 年 8 月 31 日
Thanks. Does this mean that I have to declare a random array of certain size to start with?
Daniel Shub
Daniel Shub 2011 年 8 月 31 日
In general no, you do not have to declare anything to start with.
b(5) = 20
works just fine. That said, there can be a substantial performance hit (especially for versions before r2011a) of not initiating the size beforehand. If the maximum size is reasonable, you can trim afterwards:
c = nan(1000, 1); % Initialize to the maximum size
% Some code that loops
c = c(~isnan); % Trim the unused slots

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

カテゴリ

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

タグ

タグが未入力です。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by