Convert layers of 3D matrix to list of cells

2 visualizaciones (últimos 30 días)
Stephen
Stephen el 14 de Abr. de 2014
Comentada: Stephen el 14 de Abr. de 2014
I'd like to take a 3D matrix and convert it to a list (1D) of cells.
Example:
x(:, :, 1) = magic(3);
x(:, :, 2) = 2*magic(3);
I want c{1} to be x(:, :, 1)
I want c{2} to be x(:, :, 2)
My end goal is to create a block diagonal 2D matrix of the layers of x such that,
z = [x(:,:,1), 0
0, x(:,:,2)]
or for complete...
z = [8, 1, 6, 0, 0, 0;
3, 5, 7, 0, 0, 0;
4, 9, 2, 0, 0, 0;
0, 0, 0, 16, 2, 12;
0, 0, 0, 6, 10, 14;
0, 0, 0, 8, 18, 4]
I know I can do this very easily in a loop, but there has to be a better solution. I like the following solution but it assumes that every matrix along the diagonal is the same:
blk = magic(3);
Arguments = repmat( {blk}, 1, 100 );
A = blkdiag( Arguments{:} );
Thanks in advance

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 14 de Abr. de 2014
Editada: Azzi Abdelmalek el 14 de Abr. de 2014
Edit
x(:, :, 1) = magic(3);
x(:, :, 2) = 2*magic(3);
x(:, :, 3) = 3*magic(3);
x=arrayfun(@(ii) x(:,:,ii),1:size(x,3),'un',0)
A = blkdiag( x{:} )
  3 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 14 de Abr. de 2014
Look at edited answer
Stephen
Stephen el 14 de Abr. de 2014
Wow! That is quite nifty. I'm gonna have to read up on arrayfun. Thanks a lot for your help and if think of any other options, please add them to this page.
Also, is there an easy way to reverse this task? As in take A back into original x? (assign elements of block diagonal back to layers of a 3D matrix)

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing 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