Plotting random number in a line

21 visualizzazioni (ultimi 30 giorni)
Neda
Neda il 26 Ago 2024
Commentato: Voss il 26 Ago 2024
Hi Matlab Team,
I have X = rand(1,100), and I want to plot X such that points be in X axis. At the moment, when I use plot(X, '*'), we have index 1 to 100 on x-axis and X is in the vertical axis. This is not, what I want !!!
Thanks
  1 Commento
DGM
DGM il 26 Ago 2024
Modificato: DGM il 26 Ago 2024
If you only specify a single vector input in the call to plot(), then that vector is plotted with respect to its indices. That's the way plot() works. You need both X and Y data in order to have a plot. If you only give it one vector, then it's treated as Y-data, and the X-data is implied.
If you're trying to plot X-data, then what do you expect the Y-data to be? Whatever it is, you need to supply it.

Accedi per commentare.

Risposta accettata

Voss
Voss il 26 Ago 2024
If you want to plot points on the x-axis, specify the y-coordinates as zero.
X = rand(1,100);
Y = zeros(1,100);
plot(X,Y,'*')
  4 Commenti
Aquatris
Aquatris il 26 Ago 2024
meanValue = 0.5; % mean value
stdValue = 0.15; % std value
sizeMatrix = [1 100]; % size of the random number matrix/vector
X = normrnd(meanValue,stdValue,sizeMatrix); % normally distrubeted random numbers
X = max(min(X,1),0); % clip the values at 0 and 1
hist(X,20)
Voss
Voss il 26 Ago 2024
Another option:
xmin = 0.4;
xmax = 0.6;
N = 100;
X = xmin+(xmax-xmin)*rand(1,N);
Y = zeros(1,N);
figure
subplot(2,1,1)
plot(X,Y,'*')
xlim([0 1])
subplot(2,1,2)
hist(X)
xlim([0 1])
Another option:
xmin = 0.4;
xmax = 0.6;
N = 100;
X = xmin+(xmax-xmin)*randi([0,1],1,N);
Y = zeros(1,N);
figure
subplot(2,1,1)
plot(X,Y,'*')
xlim([0 1])
subplot(2,1,2)
hist(X)
xlim([0 1])

Accedi per commentare.

Più risposte (1)

John D'Errico
John D'Errico il 26 Ago 2024
Modificato: John D'Errico il 26 Ago 2024
What is it that you want then? A plot requires TWO axes, TWO variables to be plotted, typically we might plot x versus y.
My first guess is you MIGHT want to see a histogram. I'll generate a few more points, so the histogram will look as you would expect.
X = rand(1,10000);
histogram(X,10)
Or, perhaps you might want to see just a set of vertical lines, one line for each point.
figure
X = rand(1,100);
xline(X,'r')
If you want to see something different, if these plots do not give you the information you need, then instead of telling us what you DON'T want to see, you needed to explain what you DID want to see. Otherwise all we can do is make wild guesses.

Community Treasure Hunt

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

Start Hunting!

Translated by