Borrar filtros
Borrar filtros

cell array converts to matrix, how?

161 visualizaciones (últimos 30 días)
Ken
Ken el 19 de Jul. de 2012
Editada: Dijle Kaya el 30 de Mzo. de 2021
I want to convert cell 'A' to a matrix 'B'.
A=
columns 1 through 10
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}...
columns 1 through 20
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}...
I want to have a 'B' as:
B =
1 2 3 4 5 6 7 8 9 ... 20
I tried B(1)=cell2mat(A{1}), but can I avoid doing it one by one?
Thanks!
  1 comentario
Jan
Jan el 20 de Jul. de 2012
As usual it would be helpful if you post the input data in valid Matlab syntax, such that we can try our suggestion by copy&paste them. Currently it is not clear what the "{1x1 cell}" contains. Looking at your comments it seems, like these are cell strings.

Iniciar sesión para comentar.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 19 de Jul. de 2012
B = [A{:}];
B = [B{:}];
  4 comentarios
Sean de Wolski
Sean de Wolski el 19 de Jul. de 2012
Are the contents strings?
If so, throw in a:
B = cellfun(@str2double,B);
B = [B{:}]
Ken
Ken el 19 de Jul. de 2012
Thanks!!

Iniciar sesión para comentar.

Más respuestas (1)

Greg Heath
Greg Heath el 20 de Jul. de 2012
The expressions converting a "row cell of cells" to the corresponding "row vector" are
B = [ A{ : } ]
C = [ B{ : } ]
The expression for converting a 2-D cell of cells to the corresponding 2-D matrix is
C = repmat(cell2mat( [ A{:} ] ),size(A))
  5 comentarios
Rachel Clark
Rachel Clark el 12 de En. de 2021
is there a way to make this work for a cell array containing cell arrays of different sizes?
Dijle Kaya
Dijle Kaya el 30 de Mzo. de 2021
Editada: Dijle Kaya el 30 de Mzo. de 2021
Maybe so:
a = strings(1,length(A)) % or a = zeros(1,length(A)), if A is an integerarray
for i = 1:length(A)
a(1,i) = (convertCharsToStrings(A{i})) %if A is a Stringarray
end

Iniciar sesión para comentar.

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by