How to display hidden element in matrix when we add the coordinate?

3 ビュー (過去 30 日間)
Sahil Bajaj
Sahil Bajaj 2015 年 11 月 29 日
コメント済み: Sahil Bajaj 2015 年 11 月 30 日
I have
x =
1 0 0
0 1 0
0 0 1
y=
. . .
. . .
. . .
I used y to hide elements from x as in y=x but they look like dots as I wanted to hide the elements
I want to reveal hidden elements by entering coordinates
Example
If I enter (2,2)
I want y to be displayed as
y =
. . .
. 1 .
. . .
Also,
If I were to reveal neighboring elements if x = 1, how would I do that
  4 件のコメント
Walter Roberson
Walter Roberson 2015 年 11 月 30 日
Are you trying to program the game "minesweeper" ?
Sahil Bajaj
Sahil Bajaj 2015 年 11 月 30 日
kinda

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

回答 (1 件)

Guillaume
Guillaume 2015 年 11 月 29 日
編集済み: Guillaume 2015 年 11 月 29 日
It would help if you used valid matlab syntax in your question, so we don't have to guess whether your input is a cell array, a character array or something else. It would also help if you'd said what coordinate system you use ([row, column], [x, y]?).
Possibly, this is what you want:
%input variables:
x = [1 0 0; 0 1 0; 0 0 1];
y = repmat('.', [3 3]); %assume y is a char array. Maybe it's a cell array?
coords = [2 2]; %assume that first value is row, 2nd value is column?
%replace dot at coord by 1
y(coords(1), coords(2)) = '1';
%reveal neighbours where x == 1
%step 1 find bounding box. Be careful of edge, hence min/max below:
top = max(1, coords(1)-1);
bottom = min(size(y, 1), coords(1)+1);
left = max(1, coords(2)-1);
right = min(size(y, 2), coords(2)+1);
%step 2 extract bounding box from x and y
suby = y(top:bottom, left:right);
subx = x(top:bottom, left:right);
%step 3 replace neighbour that are 1 in x by '1' in y
suby(logical(subx)) = '1';
%step 4 put bounding box back in y
y(top:bottom, left:right) = suby;
However, it sounds to me that you're lacking basic knowledge of indexing in matlab, so before you try to write an interactive game, I would advise to first learn the basics of matlab.

カテゴリ

Help Center および File ExchangeConway's Game of Life についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by