Tips on grids and matrix
3 views (last 30 days)
Show older comments
Very confused on how to go about this:
I need to write a script for a "rock paper scissors game" that creates:
- a grid of 2D points with integer values, varying from 1 to N.
- an NxN matrix A that represents the initial distribution of the three populations (each population being "rock", "paper", or "scissors")
Not looking for answers, just point me in the right direction please! i am awful at matlab
0 Comments
Answers (1)
Arjun
on 30 May 2025
As I understand it, you need a 2D grid of integer values and an N×N matrix populated with a random distribution of "rock", "paper", and "scissors".
To create a 2D grid of points with values ranging from 1 to N, you can start by initializing an N×N cell array using MATLAB’s "cell" function. Then, use nested "for" loops to populate the grid with integer values. Please refer to the code segment below:
N=4;
grid = cell(N, N);
for i = 1:N
for j = 1:N
grid{i, j} = [i, j];
end
end
disp(grid);
To generate the desired matrix with the initial distribution, you can again create an N×N cell array and use nested for loops to fill it. A random number generator such as "randi" can be used to assign values from 1 to 3, where 1 represents "rock", 2 represents "paper", and 3 represents "scissors". Please refer to the code section below:
N=4;
populations = {'rock', 'paper', 'scissors'};
A = cell(N, N);
for i = 1:N
for j = 1:N
A{i, j} = populations{randi(3)};
end
end
disp(A);
I hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!