MATLAB: Q is a 3D array of size 3x3xn. I want to use the next value of Q i.e. Q(:,:,2) and so on, every time j is even. Is there a way to do it in MATLAB.

for loopif statement

for j=1:length(p)
stress(:,:,j)=(Q(:,:,1)*strain(:,:,j));
if mod(j, 2) == 0
% j is even
else
% j is odd
end
end

Best Answer

If I understood correctly,
for j = 1:numel(p)
stress(:, :, j) = Q(:, :, 1+floor(j/2)) * strain(:, :, j);
end