How to display hidden element in matrix when we add the coordinate?
Show older comments
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 Comments
Geoff Hayes
on 29 Nov 2015
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
on 29 Nov 2015
Walter Roberson
on 30 Nov 2015
Are you trying to program the game "minesweeper" ?
Sahil Bajaj
on 30 Nov 2015
Answers (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.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!