Creating a cell array of size n
이전 댓글 표시
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.
채택된 답변
추가 답변 (3개)
Jan
2012년 7월 5일
C = cell(1, n);
C(:) = {'red'};
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.
2012년 7월 5일
I'm not sure but try also this :
n = 100
repmat( {'red'}, 1, n )
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
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
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!