Creating a 3-dim matrix from an array and a 2-dim matrices array

1 visualización (últimos 30 días)
I am trying to create a 3-dimensional matrix (e.g. 80 x 300 x 350) from an array (dim 80) and an array of 80 300 x 350 matrices. The second dimension in matrices may vary, so it needs to be padded by 0.
Any hint is welcome.
I have tried this:
for i=1:80
res(i,:,:)=cell2mat(s1(i));
end
and got this:
Subscripted assignment dimension mismatch.
Thank you
  2 comentarios
James Tursa
James Tursa el 22 de Abr. de 2015
If you are using the 300 x 350 slices downstream in your code, you might consider making those the first two dimensions of your 3D matrix so the slices are contiguous in memory. E.g., a 300 x 350 x 80 array.
Valentina Baljak
Valentina Baljak el 22 de Abr. de 2015
thanks for the hint, James, might be useful in the future

Iniciar sesión para comentar.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 22 de Abr. de 2015
Close! but using :,: means that it must fill the whole thing, not pad with zeros. Instead, only fill to the size:
for i=1:80
sii = cell2mat(s1(i));
res(i,1:size(sii,1),1:size(sii,2)) = permute(sii,[3 1 2]); % permute to write orientation
end
  1 comentario
Valentina Baljak
Valentina Baljak el 22 de Abr. de 2015
Thank you, that looks like shorter solution. In a meanwhile, I have tried more direct approach, using the coder logic, I guess.
%zero 3D matrix, placeholder predict=zeros(80, 300, 350);
for i=1:80 temp=cell2mat(s1(i)); dims=size(temp); d=dims(2); for j=1:300 for k=1:d predict(i,j,k)=temp(j,k); end end end
and it works well enough.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by