Creating a cell array of size n
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (3)
Jan
el 5 de Jul. de 2012
C = cell(1, n);
C(:) = {'red'};
F.
el 5 de Jul. de 2012
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 comentarios
F.
el 5 de Jul. de 2012
I'm not sure but try also this :
n = 100
repmat( {'red'}, 1, n )
Giuseppe Degan Di Dieco
el 19 de Mayo de 2021
Dear F,
thanks for your explanation of the cell array object.
Actually, it was quite tricky to understand.
Best.
grapevine
el 5 de Jul. de 2012
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 comentarios
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!