How to display hidden element in matrix when we add the coordinate?
이전 댓글 표시
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
Geoff Hayes
2015년 11월 29일
Sahil - please clarify what you mean by you hid with y and provide more details on how this relates to the coordinate (2,2). (What problem are you trying to solve?)
Sahil Bajaj
2015년 11월 29일
Walter Roberson
2015년 11월 30일
Are you trying to program the game "minesweeper" ?
Sahil Bajaj
2015년 11월 30일
답변 (1개)
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.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!