Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

How to create doubles from cell arrays?

1 visualización (últimos 30 días)
gsourop
gsourop el 27 de Abr. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
I would like to ask if there is a more efficient code to do the following task:
a = cell(10,1);
for i = 1 : 10
a{i,1} = randn(200,5);
end
for j =1:5
b{j} = [a{1,1}(:,j) a{2,1}(:,j) a{3,1}(:,j) a{4,1}(:,j) a{5,1}(:,j)];
end
Thank you!

Respuestas (1)

Guillaume
Guillaume el 27 de Abr. de 2018
Editada: Guillaume el 27 de Abr. de 2018
if there is a more efficient code
Yes, don't use cell arrays!
a = randn(200, 5, 10);
your b{j} is simply
permute(a(:, j, :), [1 3 2])
%or
squeeze(a(:, j, :))
Even simpler, move the j to the third dimension
a = randn(200, 10, 5);
%b{j} would simply be a(:, :, j)
  2 comentarios
gsourop
gsourop el 27 de Abr. de 2018
The initial matrices are of this form, I just use randn values in order to replicate the experiment. It is not that I am now starting the calculations. So I have to achieve result 'b' given the form of 'a'.
Guillaume
Guillaume el 27 de Abr. de 2018
Yes, I also used randn purely for demonstration.
Don't use cell arrays if you don't have to. It complicates everything. Since the matrices in your cell array are all the same concatenate them into a 3D matrix and use that from then on.
%a: a cell array of MxN matrices;
bettera = permute(cat(3, a{:}), [1 3 2])
From then on, whenever you wanted to use b{j}, use
bettera(:, :, j)
It is even possible that whatever you wanted to do with each b{j] could be done at once on bettera.

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by