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 Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by