필터 지우기
필터 지우기

Dynamically update cell without clearing previous content?

조회 수: 2 (최근 30일)
Xh Du
Xh Du 2017년 6월 15일
댓글: Image Analyst 2017년 6월 15일
Hi all,
I'd like to dynamically update cell content and size, without clearing the previous cell content. Check the following code:
clear; clc;
nr = 1;
for i = 1:5
a = cell(1, nr);
a{:, i} = {nr};
nr = nr + 1;
end
,
>> a
a =
[] [] [] [] {1x1 cell}
>> a{5}
ans =
[5]
Imagine I do not know how many iterations I will do, meaning cell size of 'a' needs to be dynamically updated with the number of iterations (which is why I use a = cell(1, nr) in the loop). However, after each iteration, content of 'a' is cleared, so I get only the last cell element of 'a'. But what I want is:
>> a
a =
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}
>> [a{:}]
ans =
[1] [2] [3] [4] [5]
I cannot move a = cell(1, nr) out of the for loop because in my real work I do not know how many iterations I will do. Any ideas? Thanks!
  댓글 수: 5
Xh Du
Xh Du 2017년 6월 15일
If the size is dynamically calculated, why would I need to delete any empty cells at the end then?
Adam
Adam 2017년 6월 15일
I mean the upper bound estimate. Based on knowledge of your algorithm you should be able to come up with some calculation that at least estimates the maximum size. If your algorithm can potentially run to infinity then that is a problem in itself.
Obviously the upper bound will leave lots of empty cells in a realistic run though.

댓글을 달려면 로그인하십시오.

채택된 답변

Xh Du
Xh Du 2017년 6월 15일
solution: 'a' can be dynamically updated without special treatment:
clear; clc;
nr = 1;
a = cell(1);
for i = 1:5
a{i} = {nr};
nr = nr + 1;
end
  댓글 수: 3
Xh Du
Xh Du 2017년 6월 15일
Yes, it is growing a cell array, not a cell, I should be more careful. Indeed what I wanted was to throw matrices with different size into a cell array.
This cell array growing process, is it slow? MATLAB does not ask me to pre-allocate memory for it but I guess as it dynamically grows, it's inefficient because of no pre-allocation?
Image Analyst
Image Analyst 2017년 6월 15일
I'm not sure how much preallocation would help, because it doesn't know the size, unless you made a guess. Allocating a bunch of empty cells with the cell() function would not help, I don't think. But maybe if you could preallocate cells with some array in them. Anyway, cells are very slow and inefficient and memory hogs but by their nature. Just run "whos" on a cell array and a table or double containing the very same numbers and you'll find the cell array can be many times larger.
rows = 2000;
m = rand(rows);
whos m
s = 2;
d = s * ones(rows/s, 1);
ca = mat2cell(m, d, d);
whos ca
In the above example, the double array is 32 MB while the cell array with the same numbers is 144 MB.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by