How to display two similar functions on a graph?
1 view (last 30 days)
Show older comments
Matt Amador
on 26 Oct 2017
Commented: Walter Roberson
on 27 Oct 2017
Hello there. I am having a bit of confusion with some code that I made, and I just need some clarification.
So I made a function here, designed to sort random numbers from an array:
function[out] = bubbleSort(a)
a = input('Enter an array of numbers to sort: ');
x = rand(1,a);
nvals = length(x);
for ii = 1:nvals-1
iptr = ii;
for jj = ii+1:nvals
if x(jj) < x(iptr)
iptr = jj;
end
end
if ii ~= iptr
temp = x(ii);
x(ii) = x(iptr);
x(iptr) = temp;
end
end
out = x;
disp(out)
And I made a call out script that utilizes the standard 'sort' function in MATLAB, and the objective is to plot the two sorts into a logarithmic plot like so:
clc
clear
a = input('Enter the array of numbers to sort: ');
x = rand(1,a);
matlabsorter = sort(x);
semilogx(matlabsorter)
hold on
semilogx(bubbleSort)
grid on
ylabel('Array Size')
xlabel('Elapsed Time')
legend('matlabsorter','bubbleSort')
hold on
The thing is however, when this executes, the two plots look identical and I'm not so sure it that's how it's supposed to be. Could anyone give any input on this?
0 Comments
Accepted Answer
Roger Stafford
on 27 Oct 2017
You could use different 'linespecs' in the plots, say 'y0' on one and 'r*' on the other. If the two sort results are the same, the asterisks should be centered inside the circles.
2 Comments
Walter Roberson
on 27 Oct 2017
semilogx(matlabsorter, 'y0');
hold on
semilogx(bubbleSort, 'r*');
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!