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

8 Ansichten (letzte 30 Tage)
zshockz
zshockz am 14 Dez. 2016
Kommentiert: Rena Berman am 12 Okt. 2020
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 Kommentare
Stephen23
Stephen23 am 6 Aug. 2020
Original question by Drew Closner on 14th December 2016 retrieved from Google Cache:
"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 Drew Closner on 14th December 2016 retrieved from Google Cache:
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

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 14 Dez. 2016
just
a = rand(M,N) > .2;
  1 Kommentar
Image Analyst
Image Analyst am 25 Dez. 2016
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.

Melden Sie sich an, um zu kommentieren.


michio
michio am 14 Dez. 2016
Bearbeitet: michio am 14 Dez. 2016
M = 5;
N = 4;
a = rand(M,N);
a(a<=0.2) = 0;
a(a>0.2) = 1;

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by