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

8 vues (au cours des 30 derniers jours)
zshockz
zshockz le 14 Déc 2016
Modifié(e) : Staff 3 le 2 Sep 2025
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 commentaires
Stephen23
Stephen23 le 6 Août 2020
Modifié(e) : Staff 3 le 2 Sep 2025
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 le 12 Oct 2020
(Answers Dev) Restored edit

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 14 Déc 2016
just
a = rand(M,N) > .2;
  1 commentaire
Image Analyst
Image Analyst le 25 Déc 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.

Connectez-vous pour commenter.

Plus de réponses (1)

michio
michio le 14 Déc 2016
Modifié(e) : michio le 14 Déc 2016
M = 5;
N = 4;
a = rand(M,N);
a(a<=0.2) = 0;
a(a>0.2) = 1;

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by