coding the way of selection between two matrices
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2014-11-23
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.
采纳的回答
Roger Stafford
2014-11-23
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);
0 个评论
更多回答(2 个)
Image Analyst
2014-11-23
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 个评论
Image Analyst
2014-11-23
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
2014-11-23
2 个评论
Roger Stafford
2014-11-23
编辑:Roger Stafford
2014-11-23
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?
另请参阅
类别
在 Help Center 和 File 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!