Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Is there a simple way to create a 3D array in which each layer is initialized with a given 2D array? I could use the following for loop, but it's very slow.
for
a = rand(1000); b = zeros(1000, 1000, 1000); for i = 1:1000 b(:,:,i) = a; end
repmat(a,1,1,1000)
rand(1000,1000,1000)
repmat
You can use repmat or repelem equivalently here
repelem
a = rand(10,10); b = repmat( a, 1, 1, 1000 ); c = repelem( a, 1, 1, 1000 );
You can also use implicit expansion, although it's not as clear to read
d = a + zeros( 1, 1, 1000 );
Add a comment
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
repmat(a,1,1,1000)solves the problem!rand(1000,1000,1000)repmat?