Create a random vector only with positive numbers

8 ビュー (過去 30 日間)
Marta
Marta 2022 年 9 月 2 日
コメント済み: Marta 2022 年 9 月 2 日
Hello, I'm new in matlab and I want to create a vector only with positive numbers.
I usually use the function "randn()" but i can't control the sign of the element.
Thank you! :)

採用された回答

Matt J
Matt J 2022 年 9 月 2 日
編集済み: Matt J 2022 年 9 月 2 日
If you don't care what the distribution is, infinite possible recommendations can be made. You can use a classically non-negative distribution, e.g., rand, poissrnd, randi,...
Or, you can improvise and do things like,
x=randn(1,5).^2
x = 1×5
1.2926 0.2196 0.1405 2.3377 0.0006
or
x=abs(randn(1,5))
x = 1×5
0.7789 1.6381 0.6319 0.5479 1.2840
  1 件のコメント
Marta
Marta 2022 年 9 月 2 日
I didn't know these functions!
Really helpful, thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by