How can I repeat a 2-D array to create a 3-D array?

A is a 2x3 array. I want to create a 3-D 'stack' so that each layer of the 3-D stack is identical to A. I've found that the following code gives the desired result:
A =[1 2 3;4 5 6];
for j=1:5
B(j,:,:)=A;
end
disp (squeeze(B(3,:,:))) % an example showing that any layer of the 3-D array is the same as A
Is there a more elegant way to do this? I tried using repmat but couldn't get the same result.

5 comentarios

It would be better to have your 2D pages in the first two dimensions. I.e., B(:,:,j) instead of B(j,:,:). That way the 2D slices are contiguous in memory, and there are many functions in MATLAB and the FEX file exchange that naturally work with "stacked" 3D arrays where the 2D slice is the first two dimensions. E.g., pagemtimes( ).
Thanks James. That's good advice. Unfortunately, I'm constrained by using a function that requires the data in this format. I guess that I could reshape the array before using the function but being consistent beforehand helps me to follow the script.
James Tursa
James Tursa el 12 de Jul. de 2022
Editada: James Tursa el 13 de Jul. de 2022
Understood. But if you are stuck with this poor design, note that it will cost you in cumbersome code downstream. E.g., everytime you pull a 2D slice from the array via B(j,:,:) it will be a 1xMxN 3D array and not strictly a 2D matrix. So you will be forced to reshape it or squeeze it into a 2D matrix just to do simple 2D stuff like use it in a matrix multiply. You may be forced to add in a bunch of reshaping etc. code just to deal with this. Rewriting the function will pay dividends downstream in your code if you have access to the source code and can do it. E.g., then simple repmat and extractions work easily:
A =[1 2 3;4 5 6];
N = 3;
B = repmat(A,1,1,N)
B =
B(:,:,1) = 1 2 3 4 5 6 B(:,:,2) = 1 2 3 4 5 6 B(:,:,3) = 1 2 3 4 5 6
B(:,:,2)
ans = 2×3
1 2 3 4 5 6
Thanks again, James. You're right. Damn, I wish I'd thought about this earlier. I will spend some time seeing if I can re-write my scripts
Stephen23
Stephen23 el 13 de Jul. de 2022
"That way the 2D slices are contiguous in memory..."
which also means that James Tursa's recommended approach will be more efficient (assuming that you mostly want to access those matrices).

Iniciar sesión para comentar.

 Respuesta aceptada

A =[1 2 3;4 5 6];
B = repmat(reshape(A, [1 size(A)]),[5 1 1])
B =
B(:,:,1) = 1 4 1 4 1 4 1 4 1 4 B(:,:,2) = 2 5 2 5 2 5 2 5 2 5 B(:,:,3) = 3 6 3 6 3 6 3 6 3 6

2 comentarios

Steve Francis
Steve Francis el 13 de Jul. de 2022
Editada: Steve Francis el 13 de Jul. de 2022
Thanks very much!
Bruno Luong
Bruno Luong el 13 de Jul. de 2022
Editada: Bruno Luong el 13 de Jul. de 2022
EDIT : my deleted comment moves here
If you do repeat 3rd dimension few other approaches
A =[1 2 3;4 5 6]; N = 2;
B = repmat(A,[1,1,N]), % James's comment
B =
B(:,:,1) = 1 2 3 4 5 6 B(:,:,2) = 1 2 3 4 5 6
B = repelem(A,1,1,N),
B =
B(:,:,1) = 1 2 3 4 5 6 B(:,:,2) = 1 2 3 4 5 6
B = A(:,:,ones(1,N)),
B =
B(:,:,1) = 1 2 3 4 5 6 B(:,:,2) = 1 2 3 4 5 6
B = A + zeros(1,1,N),
B =
B(:,:,1) = 1 2 3 4 5 6 B(:,:,2) = 1 2 3 4 5 6

Iniciar sesión para comentar.

Más respuestas (1)

% what you have now:
A =[1 2 3;4 5 6];
for j=1:5
B(j,:,:)=A;
end
% another way, using repmat and permute:
B_new = repmat(permute(A,[3 1 2]),5,1);
% the result is the same:
isequal(B_new,B)
ans = logical
1

2 comentarios

Thank you, Voss.
Voss
Voss el 13 de Jul. de 2022
You're welcome!

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 12 de Jul. de 2022

Comentada:

el 13 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by