loop over a cell and combine cell columns into one column

1 visualización (últimos 30 días)
bbah
bbah el 22 de Nov. de 2019
Editada: bbah el 22 de Nov. de 2019
i have a cell array e.g
[1,10,18,24,31,40,0] [] 2.0673943 2.0874443 2.1595383 2.2108727 2.2004662 2.1298561 []
[2,11,19,25,32,41,49,-1] [] 2.5844460 1.4922142 1.9188881 2.1945801 2.1078224 2 2.0430222
[3,12,20,26,33,43,-1] [] 2.4619789 2.1192665 2.1591911 2.2104111 2.2032127 2.1415558 []
[4,13,21,27,34,-1] [] 2.7252617 1.4234924 1.8084259 1.9428711 2.0691185 [] []
i want to take every column and combine them together into a row vector into the 2nd column of my cell.
i did this but i cant find my mistake. my code takes every column and combines it into the 2nd column but in the next cell he takes the previous ones too.
for i = 1:length(element_row)
for k = 1:length(element_row{i,1})
Abstand = element_row{i,k+1};
row = [row,Abstand];
element_row{i,2} = row;
end
my output is:
[1,10,18,24,31,40,0] [2.0673943,2.0874443,2.1595383,2.2108727,2.2004662,2.1298561] 2.0673943 2.0874443 2.1595383 2.2108727 2.2004662 2.1298561 []
[2,11,19,25,32,41,49,-1] 1x13 single 2.5844460 1.4922142 1.9188881 2.1945801 2.1078224 2 2.0430222
[3,12,20,26,33,43,-1] 1x19 single 2.4619789 2.1192665 2.1591911 2.2104111 2.2032127 2.1415558 []
[4,13,21,27,34,-1] 1x24 single 2.7252617 1.4234924 1.8084259 1.9428711 2.0691185 [] []
but i want
[1,10,18,24,31,40,0] [2.0673943,2.0874443,2.1595383,2.2108727,2.2004662,2.1298561] 2.0673943 2.0874443 2.1595383 2.2108727 2.2004662 2.1298561 []
[2,11,19,25,32,41,49,-1] [2.5844460,1.4922142,1.9188881,2.1945801,2.1078224,2,2.0430222] 2.5844460 1.4922142 1.9188881 2.1945801 2.1078224 2 2.0430222
[3,12,20,26,33,43,-1] [2.4619789,2.1192665,2.1591911,2.2104111,2.2032127,2.1415558] 2.4619789 2.1192665 2.1591911 2.2104111 2.2032127 2.1415558 []
[4,13,21,27,34,-1] [2.7252617,1.4234924,1.8084259,1.9428711,2.0691185] 2.7252617 1.4234924 1.8084259 1.9428711 2.0691185 [] []

Respuesta aceptada

Stephen23
Stephen23 el 22 de Nov. de 2019
Editada: Stephen23 el 22 de Nov. de 2019
Where C is your cell array:
>> C(:,2) = cellfun(@cell2mat,num2cell(C(:,3:end),2),'uni',0)
C =
[1x7 double] [1x6 double] [2.0674] [2.0874] [2.1595] [2.2109] [2.2005] [2.1299] []
[1x8 double] [1x7 double] [2.5844] [1.4922] [1.9189] [2.1946] [2.1078] [ 2] [2.043]
[1x7 double] [1x6 double] [ 2.462] [2.1193] [2.1592] [2.2104] [2.2032] [2.1416] []
[1x6 double] [1x5 double] [2.7253] [1.4235] [1.8084] [1.9429] [2.0691] [] []
Checking the contents of each cell in the second column:
>> C{:,2}
ans =
2.0674 2.0874 2.1595 2.2109 2.2005 2.1299
ans =
2.5844 1.4922 1.9189 2.1946 2.1078 2 2.043
ans =
2.462 2.1193 2.1592 2.2104 2.2032 2.1416
ans =
2.7253 1.4235 1.8084 1.9429 2.0691
  1 comentario
bbah
bbah el 22 de Nov. de 2019
Editada: bbah el 22 de Nov. de 2019
Error using cell2mat (line 45)
All contents of the input cell array must be of the same data type.
should i change the data type ? if so how can i change them to e.g. single or double.
EDIT: okay i got it.
i just converted my input data few steps ahead into "double-precision" now it works
thank you very much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and 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