How do I write a script that creates an M x N array of random numbers?

7 ビュー (過去 30 日間)
zshockz
zshockz 2016 年 12 月 14 日
編集済み: Staff 3 2025 年 9 月 2 日
So I need to write a script that creates an M X N array of random numbers. Move through the array, element-by-element, and set any value that is less than 0.2 to 0, and any value that is greater than (or equal to) 0.2 to 1.
  3 件のコメント
Stephen23
Stephen23 2020 年 8 月 6 日
編集済み: Staff 3 2025 年 9 月 2 日
Original question by original author:
"How do I write a script that creates an M x N array of random numbers?"
So I need to write a script that creates an M X N array of random numbers. Move through the array, element-by-element, and set any value that is less than 0.2 to 0, and any value that is greater than (or equal to) 0.2 to 1.
Original comment by original author:
I figured it out!
Here is the answer if anyone needs it:
a = rand (4,5)
if a =< 0.2
a = 0
else a > 0.2
a = 1
end
Rena Berman
Rena Berman 2020 年 10 月 12 日
(Answers Dev) Restored edit

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 12 月 14 日
just
a = rand(M,N) > .2;
  1 件のコメント
Image Analyst
Image Analyst 2016 年 12 月 25 日
Depends on if "element-by-element" wanted a "for loop" solution or a vectorized solution.
If it's a homework solution I'd hope the professor would accept either way since the problem statement was so ambiguous.

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

その他の回答 (1 件)

michio
michio 2016 年 12 月 14 日
編集済み: michio 2016 年 12 月 14 日
M = 5;
N = 4;
a = rand(M,N);
a(a<=0.2) = 0;
a(a>0.2) = 1;

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by