【问题标题】:How can I compute the time it takes Matlab to do gaussian elimination for various sized matrices?如何计算 Matlab 对各种大小的矩阵进行高斯消元的时间?
【发布时间】:2015-08-18 18:24:43
【问题描述】:

这是我目前计算的平均需要多长时间的代码,MATLAB 在大小为 N=200 的矩阵上实现高斯消元:

Ntr=50; % Number of trials
N=200; % Matrix size

times=zeros(Ntr,1); % Vector of timing data for Ntr trials

for i=1:Ntr

% Form a random matrix A and right-hand side b (normally distributed)
A=randn(N,N);
b=randn(N,1);

% Apply backslash and calculate time taken
tic;
x=A\b;
times(i)=toc;

end

N
mean_time=mean(times)

如何修改此代码,以便它针对各种 N 值(例如 N=200、500、1000、3000 等)进行计算?我尝试了一个 for 循环,但 randn 只能接受标量值...... 我正在寻找的最终结果是绘制 N 值与平均所用时间的对数图。 任何帮助将不胜感激!

【问题讨论】:

  • timeit 可能是计时的首选方式。此外,您可以查看 here 以获取一些基准测试示例。
  • 什么意思?你想让Ntr 可变吗?
  • 为什么不为 N 创建第二个 for 循环?

标签: matlab for-loop matrix plot gaussian


【解决方案1】:

您应该设置一个具有您想要评估的所有大小的向量,然后在您的 for 循环中将 N 定义为 N=All_the_N(i)。这样 randn 只取一个标量值,然后迭代不同的 N。

All_the_N=[100 200 300 400]; %Matrix Size
Ntr=4;
times=zeros(Ntr,1);

for i=1:Ntr

N=All_the_N(1,i);
A=randn(N,N);
b=randn(N,1);

f=@() A/b;
times(i,1)=timeit(f)

end

【讨论】:

    猜你喜欢
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多