Effacer les filtres
Effacer les filtres

How to display two similar functions on a graph?

3 vues (au cours des 30 derniers jours)
Matt Amador
Matt Amador le 26 Oct 2017
Commenté : Walter Roberson le 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?

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by