Vectorizing access to a slice of a three-dimensional matrix in MATLAB

Viewed 85

I have a three-dimensional matrix of these sizes, approximately

A = rand(20, 1000, 20);

where the first and third dimensions are always the same length. I want to zero the elements in a main diagonal slice. This does what I mean

for ii = 1:size(A, 1)
    A(ii, :, ii) = 0;
end

Is there a vectorized or otherwise faster way to do this? This code runs about 100,000 times, with these approximate sizes, but not the exact same sizes each time.

2 Answers
Related