How do I create a random matrix of numbers with 2 unique positions?

1 回表示 (過去 30 日間)
Tyler Nelson
Tyler Nelson 2019 年 10 月 11 日
編集済み: Kevin Phung 2019 年 10 月 11 日
I'm trying to create a game in MatLab for class project, one of my goals is to use a matrix to create a map, so I need to be able to generate an matrix of numbers that has 2 unique points, either fixed, or randomized, one for the player's starting positon, or spawn point, and one for the ending point as a goal for the character, I know how to create a matrix of random numbers using the rand command, however, I have no idea how to insert 2 unique points and make sure those 2 points are always there

回答 (1 件)

Kevin Phung
Kevin Phung 2019 年 10 月 11 日
start_pt = [1 3]; % first row, third col
end_pt = [5 1]; % fifth row, first col
M = rand(5); %5x5 random map
M(start_pt(1),start_pt(2)) = some_unique_value;
M(end_pt(1),end_pt(2)) = some_other_unique_value;
is this what youre looking for?
  2 件のコメント
Tyler Nelson
Tyler Nelson 2019 年 10 月 11 日
I didn't think about that, it would work, do you know of a way to randomize the start and end points ,or should I just keep them as fixed points?
Kevin Phung
Kevin Phung 2019 年 10 月 11 日
編集済み: Kevin Phung 2019 年 10 月 11 日
I don't have too much context so I don't really know what to suggest as the most optimal method. What exactly are you randomizing? Are you randomizing INDICES for where the start/end points would be, or are you randomizing the VALUE located at those FIXED indices?
the randi() function could help you here.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStrategy & Logic についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by