Sven
Sven

Reputation: 1133

Plotting Matrix in Matlab

I had the task to plot the computational time cost of 2 algorithms in function of the size of the matrices they solved.

What I did so far was made a method that did every algorithm x amount of times and stored the times.

In the end, I've got a matrix like such:

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

With the first row being the computational cost of the 2 algorithms for a matrix of size 20x20, the second row the cost of the 2 algorithms for a matrix of size 40x40, then for 80x80, 160x160, 320x320 and 640x640.

The reason the 2 columns are nearly the same is because I've yet to write the second algorithm and just used the first algorithm 2 times.

What I now need to do is plot the cost of the 2 algorithms, in the same figure, in function of the increasing matrix size. I'm however stuck on the plot syntax and I keep failing in getting a pretty figure. Could anybody help?

Upvotes: 1

Views: 2358

Answers (3)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38042

How about

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'...
);

results:

enter image description here

Upvotes: 1

Rakshit Kothari
Rakshit Kothari

Reputation: 416

If the first column belongs to the Computation time of the first algorithm for different sizes, and the second column belongs to the Computation time of the second algorithm, the you can plot it beautifully:

assume the matrix for stored computation times is TimeComputation

figure(1)

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

hold on

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

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

Let me know if you have any further questions!

Upvotes: 0

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21561

How about just this:

plot(T)

or if you want the x values, define x and then

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

Upvotes: 0

Related Questions