フィルターのクリア

How to deal with the problem of the changing size data?

1 回表示 (過去 30 日間)
Mateusz
Mateusz 2011 年 8 月 9 日
I have the following code:
im = {};
k = 0
while not_end():
im = {im{:}, dir(filepath, fileArray(k))};
k = k + 1;
end
where we assume that the fileArray is too big so I cannot pre-allocate im using length(fileArray). Moreover, you cannot also predict when not_end() becomes false. The example itself is artificial, the point is I cannot pre-allocate the cell array im for some reasons. Is there any structure in Matlab that can be used to add elements (like linked list for example) to the structure itself without much loss of the performance?
  1 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 8 月 9 日
Saying you cannot preallocate im because length(fileArray) is too big doesn't make sense unless it's length is infinite.

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

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 8 月 9 日
What is fileArray?
You can always preallocate im as a cell array with length equal to fileArray and extract/concatenate once the loop is finished.
for k = 1:length(fileArray)
im{k} = dir(filepath, fileArray(k));
end
im = cat(1,im{:});
EDIT
You cannot do otherwise (still the concatenation is better done once and outside of the cycle):
k = 0;
while...
k = k + 1;
im{k} = dir(filepath, fileArray(k));
end
im = cat(1,im{:});
  3 件のコメント
Walter Roberson
Walter Roberson 2011 年 8 月 9 日
You didn't increment k, Oleg.
Oleg Komarov
Oleg Komarov 2011 年 8 月 9 日
Thanks for pointing that out.

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

その他の回答 (0 件)

カテゴリ

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