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
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
2023년 3월 20일
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.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!