Creating a cell array of size n

조회 수: 135 (최근 30일)
Luffy
Luffy 2012년 7월 5일
댓글: Giuseppe Degan Di Dieco 2021년 5월 19일
I need a cell array of size n, like if n is 3, I need
C = {'red','red','red'}
If n is 100,
C = {'red','red',.......'red'}(100 cells)
I tried this,
C = cell(1,n);
for i = 1:n
C(i) = 'red';
end
This gives known error of conversion to cell from char is not possible.

채택된 답변

James Tursa
James Tursa 2012년 7월 5일
편집: James Tursa 2012년 7월 5일
The most memory efficient way is the following without a loop:
C(1:n) = {'red'}; % Assuming C doesn't exist yet (EDITED)
That is because in the above line, all n copies of the 'red' string are reference copies of each other (like a shared data copy), so the memory footprint is smaller than filling each cell individually with a brand new allocated string in a loop.
Having said that, I strongly suspect that what you are attempting to do is futile. If you change any of the individual 'red' strings downstream in your code you will probably need to allocate brand new cell element variables anyway, so this pre-allocation of 'red' would be useless (actually wastes time) regardless of the methoud you use to create C in the first place (my method or with a loop). The only exceptions would be if you don't plan on altering C at all downstream, or if you plan to operate on each string "in-place" ... i.e., changing the contents of the string directly without changing its length, etc. Can you elaborate on what you intend to do with C downstream in your code?
  댓글 수: 4
Albert Yam
Albert Yam 2012년 7월 5일
See Jan Simon's answer. James just missed a '1:n'.
C(1,1:n) = {'red'};
James Tursa
James Tursa 2012년 7월 5일
P.S. My comments also apply to Jan's answer, since it essentially does the same thing.

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

추가 답변 (3개)

Jan
Jan 2012년 7월 5일
C = cell(1, n);
C(:) = {'red'};

F.
F. 2012년 7월 5일
Your error :
C(i) = 'red';
C is a cell array, with C(i) you reach the place in the cell array and not the element which is in this place. So try :
C{i} = 'red';
  댓글 수: 2
F.
F. 2012년 7월 5일
I'm not sure but try also this :
n = 100
repmat( {'red'}, 1, n )
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021년 5월 19일
Dear F,
thanks for your explanation of the cell array object.
Actually, it was quite tricky to understand.
Best.

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


grapevine
grapevine 2012년 7월 5일
You have to modify your code in this way :
C = cell(1,n);
for i = 1:n
C(i) = java.lang.String('red');
end
Another solution could pass by using the function: char2cell, which is available on Matlab Central Exchange
good luck
  댓글 수: 2
Jan
Jan 2012년 7월 5일
편집: Jan 2012년 7월 5일
java.lang.String ?! Why should this be converted to a java string? And CHAR2CELL seems not helpful also.
grapevine
grapevine 2012년 7월 9일
편집: grapevine 2012년 7월 9일
Weird! it works for me. read the doc :)

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by