フィルターのクリア

need help on my matrix code

2 ビュー (過去 30 日間)
Nathen Eberhardt
Nathen Eberhardt 2020 年 2 月 15 日
コメント済み: Nathen Eberhardt 2020 年 2 月 15 日
so pretty much i am creating minesweeper, and i created a random 10x10 matrix( mine_probabilities = rand(10) ) and then another matrix, mine(10,10) = 0 for the minefield
if the any number in rand(10) is >= .85 then the mine(m,n) = 1 ( which means there is a mine) if not mine(m,n) = 0 (no mine).
I am not sure why this is not working any help??
mine_probabilities = rand(10);
mine(10,10) = 0;
if mine_probabilities >= .85
mine = 1;
else
mine() = 0;
end
disp(mines)

採用された回答

John D'Errico
John D'Errico 2020 年 2 月 15 日
編集済み: John D'Errico 2020 年 2 月 15 日
An if statement dose NOT work this way. You cannot use that single statement to act on each element of the array, independently. However the solution is actually trivial, far simpler than you are making it.
mine_probabilities = rand(10);
mine = mine_probabilities > 0.85;
Thats all. Nothing more is needed. In fact, you never really needed to generate the array min_probabilities as a distinct array. So you could have written it in one line:
mine = rand(10) > 0.85;
You could also have written the whole thing using a loop, putting an if statement inside a loop, but why? Instead, use the capabilities of MATLAB as a matrix language to do the entire thing in one step.
  1 件のコメント
Nathen Eberhardt
Nathen Eberhardt 2020 年 2 月 15 日
Thank you so much! Works perfectly

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by