循环与向量化
(1)按列优先循环
% 4. 按列优先循环
clear all
clc
n = 1000;
a = rand(n);
tic
for i = 1:n
for j = 1:n
a(i,j);
end
end
toc
for i = 1:n
for j = 1:n
a(j,i);
end
end
toc
(2)循环次数多的变量安排在内层
% 5. 循环次数多的变量安排在内层
clear all
clc
tic
a = 0;
for i = 1:1000
for j = 50000
a = a + 1;
end
end
toc
tic
a = 0;
for i = 1:50000
for j = 1:1000
a = a + 1;
end
end
toc