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 个评论

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.
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
"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).

请先登录,再进行评论。

 采纳的回答

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 个评论

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

请先登录,再进行评论。

更多回答(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

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

版本

R2020b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by