How do I code for this random situation?

NAA · Mar 20, 2023 · 1.9K views
Question
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.  
Expert Answer
Profile picture of John Williams
John Williams PhD Expert
Answered Aug 23, 2026
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).
 
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!