Matrix with nested for loops

조회 수: 15 (최근 30일)
Sophie Stocker
Sophie Stocker 2020년 6월 19일
댓글: Sophie Stocker 2020년 6월 19일
Hello!
I have to create two 20x20 matrices in which the values should be either 0 or 1 and are randomly distributed.
In one matrix the chance of getting a 1 should be 90% and in the other one it should be 95%.
I don't really understand how the nested for loop works or how the values created in the for loop get assigned to the Matrix.
I would really appreciate your help!
Thanks in advance!

채택된 답변

James Tursa
James Tursa 2020년 6월 19일
You don't need loops for this. E.g.,
p = the probability
result = rand(20,20) < p; % your matrix of 0's and 1's
  댓글 수: 1
Sophie Stocker
Sophie Stocker 2020년 6월 19일
Thank you so much! You saved my day :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by