Tips on grids and matrix

조회 수: 5 (최근 30일)
Melissa Hickman
Melissa Hickman 2021년 4월 3일
답변: Arjun 2025년 5월 30일
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

답변 (1개)

Arjun
Arjun 2025년 5월 30일
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);
{[1 1]} {[1 2]} {[1 3]} {[1 4]} {[2 1]} {[2 2]} {[2 3]} {[2 4]} {[3 1]} {[3 2]} {[3 3]} {[3 4]} {[4 1]} {[4 2]} {[4 3]} {[4 4]}
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);
{'rock' } {'paper' } {'scissors'} {'rock' } {'paper' } {'paper' } {'paper' } {'scissors'} {'scissors'} {'paper' } {'scissors'} {'scissors'} {'rock' } {'scissors'} {'rock' } {'scissors'}
I hope this helps!

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by