coding the way of selection between two matrices

3 views (last 30 days)
Hi I have two matrices A and B. the dimension of These matrices is (m x n). I want to find the pattern of selection in which I allocate value 1 for selection and value 0 for non selection in such a way that if I select one cell for example cell(m=2 and n=3) in matrix A and allocate 1 to it, the value of 4 cells that are around it in matrix B will be 1 and the other cells will be 0. this pattern of selection must be applied for all cells in matrix A and therefore must be done for cells in matrix B.
For example if I have matrix A (3 x 3) and read it like image, if I select the first cell and allocate 1 to it. in matrix B I must allocate 1 to cells 2 and 4. I must do the pattern for all cells.
can you help me please?
  5 Comments
fatema saba
fatema saba on 23 Nov 2014
in step 1 when I select cell 1 in matrix A (it is highlighted in yellow color), cell 2 and cell 5 must be selected in matrix B. like that in step 2 when I select cell 2 in matrix A, cells 1 and 6 and 3 must be selected in matrix B. in fact when I select a cell in matrix A, cells that are around it must be selected in matrix B automatically.
Guillaume
Guillaume on 23 Nov 2014
What do you mean by 'selected'? There's no concept of selection in matlab. You could extract the elements (in what?), change their value (to what?), get their indices, etc. but select on its own doesn't mean anything.

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 23 Nov 2014
The difficulty with your question, Fatema, is that you have not made it clear what you want to happen with respect to matrices A and B over time. That is, what do you mean by the sentence "I must do the pattern for all cells." How does "do the pattern" translate to matlab action? You say "If I select the first cell and allocate 1 to it" but presumably you want matlab to carry out this selection process and you don't say how it should be done.
The nearest thing I can think of is that you want to write a script that lies within a larger code in which a choice of the location of a 1-bit in A leads to a result in B. For that, suppose 'iA' is some linear index value of a position within A. You can set up B as follows:
[m,n] = size(A);
[iB,jB] = ind2sub([m,n],iA);
B = zeros(m+2,n+2);
B(iB,jB+1) = 1; B(iB+1,jB) = 1; B(iB+2,jB+1) = 1; B(iB+1,jB+2) = 1;
B = B(2:m+1,2:n+1);

More Answers (2)

Image Analyst
Image Analyst on 23 Nov 2014
Looks like a standard "game of life" sort of thing. Perhaps the File Exchange will give you ideas: http://www.mathworks.com/matlabcentral/fileexchange/index?utf8=%E2%9C%93&term=game+of+life
  3 Comments
Image Analyst
Image Analyst on 23 Nov 2014
See http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Basically you change a element (pixel) and then surrounding elements change in a manner according to some rules you set up. Like the north, south, east, and west pixels get set to 1 (I think that's your rule). Then you can apply the rules on those pixels, or on any remaining pixels. With your rule, won't the whole array eventually be 1?
fatema saba
fatema saba on 23 Nov 2014
Yes it is exactly what I tried to explain. Thank you. but I want to find the rule in order to create the matrix that I show in this image based on images to show before. the rows of this new matrix are m x n and the columns are 2 (m x n). arrows show that the process is continued to end. of course instead of colors (yellow and white) I must use 1 and 0 respectively.

Sign in to comment.


fatema saba
fatema saba on 23 Nov 2014
thanks 4 your best recommendation. I write a script based on your comments and I write this code.
A=eye(3)
[m,n]=size(A)
B = zeros(m,n)
s=[m,n];
for r = 1:m*n
for c = 1:n*m
if r == c
iA=r
[iB,jB] = ind2sub(s,iA)
if(jB+1<n)
B(iB,jB+1) = 1
end
if(iB+1<m)
B(iB+1,jB) = 1
end
if iB-1>0
B(iB-1,jB) = 1
end
if jB-1>0
B(iB,jB-1) = 1
end
B = zeros(m,n)
end
end
end
it works correctly but only for 2 column after column 2 I cant receive my expected result. 4 example when iA=4 iB=1 , jB=2 I want the cells that showed in red circle, be 1 .but beacause my code doesn't work after column 3 it cant show the red circle in the column 3 as 1.
  2 Comments
Roger Stafford
Roger Stafford on 23 Nov 2014
Edited: Roger Stafford on 23 Nov 2014
You need to change the first two inequalities to allow equality:
if jB+1<=n
....
if iB+1<=m
....
Otherwise 1's won't be entered into either the last column or the last row.
Also why have two nested for-loops when the only action occurs when r == c? You could do the same thing with just the "for r = 1:m*n" loop alone.
And finally, that last operation "B = zeros(m,n)" would undo all your good work. What is it doing there?
fatema saba
fatema saba on 24 Nov 2014
Thank you. I want to keep the last matrix B that is created in each iteration and convert it to matrix 1x(m*n) matrix in order to combine them into a matrix (m*n)x(m*n) matrix.

Sign in to comment.

Categories

Find more on Conway's Game of Life 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!