coding the way of selection between two matrices
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
fatema saba
le 22 Nov 2014
Commenté : fatema saba
le 24 Nov 2014
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 commentaires
Guillaume
le 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.
Réponse acceptée
Roger Stafford
le 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);
0 commentaires
Plus de réponses (2)
Image Analyst
le 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 commentaires
Image Analyst
le 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
le 23 Nov 2014
2 commentaires
Roger Stafford
le 23 Nov 2014
Modifié(e) : Roger Stafford
le 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?
Voir également
Catégories
En savoir plus sur Conway's Game of Life dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!