Matlab中的绘图矩阵

Plotting Matrix in Matlab

我的任务是根据所求解的矩阵的大小来绘制2种算法的计算时间成本。

到目前为止,我所做的是使每个算法执行x次并存储时间的方法。

最后,我有一个像这样的矩阵:

1
2
3
4
5
6
7
8
T =

1.248008000000027e-003    9.360059999994519e-004
7.488048000004710e-003    1.456009333332986e-002
4.992032000000109e-002    2.808017999999492e-002
1.809611600000039e-001    1.489809550000018e-001
5.740836800000352e-001    5.865637599999672e-001
4.748670439999978e+000    4.714350220000005e+000

第一行是尺寸为20x20的矩阵的两种算法的计算成本,第二行是尺寸为40x40的矩阵的两种算法的计算成本,然后是80x80、160x160、320x320和640x640的计算成本。 >

两列几乎相同的原因是因为我尚未编写第二种算法,并且只使用了第一种算法两次。

我现在需要做的是在同一图中绘制这两种算法的成本,以增加矩阵大小为函数。
但是,我仍然坚持使用绘图语法,并且始终无法获得漂亮的图形。
有人可以帮忙吗?


怎么样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
T = [
    1.248008000000027e-003    9.360059999994519e-004
    7.488048000004710e-003    1.456009333332986e-002
    4.992032000000109e-002    2.808017999999492e-002
    1.809611600000039e-001    1.489809550000018e-001
    5.740836800000352e-001    5.865637599999672e-001
    4.748670439999978e+000    4.714350220000005e+000];


figure, hold on

 % column 1
plot(1:size(T,1), T(:,1), 'r.-');

% column 2
plot(1:size(T,1), T(:,2), 'b.-');

% nicer labels at the X-tick locations
set(gca, 'xticklabel', {...
    '20×20',...
    '40×40',...
    '80×80',...
    '160×160',...
    '320×320',...
    '640×640'}...
);

% finish plot
grid on
ylabel('Execution time required [s]')
xlabel('Matrix size [-]')

legend(...
    'Algorithm 1',...
    'Algorithm 2',...
    'location', 'NorthWest'...
);

结果:

enter


如果第一列属于不同大小的第一种算法的计算时间,而第二列属于第二种算法的计算时间,则可以绘制精美的图:

假设存储的计算时间的矩阵为TimeComputation

1
2
3
4
5
6
7
8
9
figure(1)

plot(TimeComputation(:,1),'-.r')

hold on

plot(TimeComputation(:,2),'--.b')

legend('Function 1','Function 2')

让我知道您是否还有其他问题!


这又如何:

1
plot(T)

或者如果需要x值,请先定义x,然后

1
2
3
plot(x,T(:,1))
hold all
plot(x,T(:,2))