Azzera filtri
Azzera filtri

How to display two similar functions on a graph?

3 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Roger Stafford
Roger Stafford il 27 Ott 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 Commenti
Matt Amador
Matt Amador il 27 Ott 2017
I'm not sure what you mean. Can you show an example of what you are trying to say?
Walter Roberson
Walter Roberson il 27 Ott 2017
semilogx(matlabsorter, 'y0');
hold on
semilogx(bubbleSort, 'r*');

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by