Andi
Andi

Reputation: 4855

MATLAB: Insert leading rows in every slice of 3D matrix

I need to insert some leading zeros in every slice of a 3D matrix. I am wondering whether there is a more elegant way rather than using a for-loop, such as this one:

orig3D = rand(5,1,2);

for n = 1 : 2
    new3D(:,:,n) = [zeros(3,1); orig3D(:,:,n)];
end

Upvotes: 2

Views: 20

Answers (1)

etmuse
etmuse

Reputation: 505

The code can be vectorised, avoiding the loop:

new3D = [zeros(3,1,2); orig3D];

Upvotes: 3

Related Questions