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" ?

댓글을 달려면 로그인하십시오.

답변 (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 CenterFile Exchange에서 Conway'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