How to extract a matrix of values from cell array of cell arrays of structs
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jerry Guern
el 28 de Feb. de 2024
Comentada: Jerry Guern
el 5 de Mzo. de 2024
I have an array of array of structs, and I'd like to extract the value 'metric' from each, into a simple matrix. cell2mat and other functions just gave me various errors, so I wrote the following give-up code. It works, but is there a better way?
msea = zeros(n1,n2);
for i=1:n1
for j=1:n2
msea(i,j)=mse{i}{j}.metric;
end
end
3 comentarios
Bruno Luong
el 29 de Feb. de 2024
Your data is not array of array of structs but cell array of cell array of structs. That's are not convenient to organize the data.
You might use for example 2D struct array if they have the same fields.
Respuesta aceptada
Bruno Luong
el 1 de Mzo. de 2024
Generate data (Thanks Voss)
n1 = 3;
n2 = 4;
mse = cell(1,n1);
for ii = 1:n1
mse{ii} = cell(1,n2); % row vector
for jj = 1:n2
mse{ii}{jj} = struct('metric',ii+(jj-1)*n1);
end
end
Single line code
reshape([cell2mat(cat(1,mse{:})).metric],length(mse),[])
2 comentarios
Bruno Luong
el 1 de Mzo. de 2024
Movida: Bruno Luong
el 1 de Mzo. de 2024
I would first convert to struct array S
S = cell2mat(cat(1,mse{:}));
Then use S from now on and forget about mse.
metric_array = reshape([S.metric], size(S))
Más respuestas (1)
Walter Roberson
el 28 de Feb. de 2024
Movida: Walter Roberson
el 28 de Feb. de 2024
msea = zeros(n1,n2);
for i=1:n1
msea(i,1:n2) = [mse{i}{1:n2}.metric];
end
4 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing 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!