Plotting random number in a line

25 views (last 30 days)
Neda
Neda on 26 Aug 2024
Commented: Voss on 26 Aug 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 Comment
DGM
DGM on 26 Aug 2024
Edited: DGM on 26 Aug 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.

Sign in to comment.

Accepted Answer

Voss
Voss on 26 Aug 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 Comments
Aquatris
Aquatris on 26 Aug 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 on 26 Aug 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])

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 26 Aug 2024
Edited: John D'Errico on 26 Aug 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!