How do I code for this random situation?

On a r*c size grid, 55% of the sites are randomly filled with X, 2% randomly filled with Y, and the rest are empty

3 件のコメント

John D'Errico
John D'Errico 2023 年 3 月 20 日
Geez. WIll you stop changing your questions as fast as you can? I commented on your last question. Then I answered this one, in its previous incarnation, only to see you had comepletely changed the question. I deleted my answer to the last question, and now I'm done.
NAA
NAA 2023 年 3 月 20 日
There was no previous question like this, and I saw your comment on the other question, you didn't answer. Instead you just left a rude comment like the one here
Image Analyst
Image Analyst 2023 年 3 月 20 日
@NAA, OK try it this way:
r = 10;
c = 20;
output = nan(r, c);
numX = round(0.55 * r * c) % Number of elements to place an X into.
numY = round(0.02 * r * c) % Number of elements to place a Y into.
X = 1;
Y = 2;
output(1 : numX) = X;
output(numX + 1 : numX + numY) = Y;
randomIndexes = randperm(numel(output));
output = reshape(output(randomIndexes), [r, c])
There are other ways that would work also.

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

 採用された回答

Image Analyst
Image Analyst 2023 年 3 月 20 日

1 投票

This looks like a homework problem. If you have any questions ask your instructor or read the link below to get started:
Obviously we can't give you the full solution because you're not allowed to turn in our code as your own.
Hint:
output = nan(r, c);
numX = round(0.55 * r * c); % Number of elements to place an X into.
numY = round(0.02 * r * c); % Number of elements to place a Y into.
Two ways to do it:
  1. You can do it vectorized using randperm (assign the X to the first numX elements, then assign Y to the next numY elements, then use randperm to scramble the order), or
  2. you can do it brute force using a for loop to place the x value and another loop to place the Y value, but only in the location if the value is a nan (not an X).

1 件のコメント

NAA
NAA 2023 年 3 月 20 日
it's not the hw problem, this is just part of an assumption I am making to get started, but thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeParticle & Nuclear Physics についてさらに検索

製品

リリース

R2022b

質問済み:

NAA
2023 年 3 月 20 日

コメント済み:

2023 年 3 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by