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

댓글을 달려면 로그인하십시오.

채택된 답변

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 CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by