One approach —
N = 5;
tic
[m,n] = ndgrid(1:N);
A = 2*N + 1 - (m + n)
toc
Compare —
tic
A = zeros(N);
for m = 1:N
for n = 1:N
A(m, n) = 2*N + 1 - (m + n);
end
end
toc
A
The nested loops are actually faster!
.
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!