Plotting random number in a line

26 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by