Plotting random number in a line

조회 수: 25 (최근 30일)
Neda
Neda 2024년 8월 26일
댓글: Voss 2024년 8월 26일
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
DGM
DGM 2024년 8월 26일
편집: DGM 2024년 8월 26일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2024년 8월 26일
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
Aquatris
Aquatris 2024년 8월 26일
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 2024년 8월 26일
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])

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2024년 8월 26일
편집: John D'Errico 2024년 8월 26일
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.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by