Filter löschen
Filter löschen

How to display two similar functions on a graph?

3 Ansichten (letzte 30 Tage)
Matt Amador
Matt Amador am 26 Okt. 2017
Kommentiert: Walter Roberson am 27 Okt. 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?

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by