how i can make change on middle band of specific matrix?
Mostrar comentarios más antiguos
if i have matrix of the size n*n how i can make change on the middle band only assume this matrix
matt=[1 1 1 1;1 1 1 1;1 1 1 1;1 1 1 1 ]
matt =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
and I want to make the middle band all zeros?does it change if the size n*m
in other word how i can manipulate middle band of any image?(this is a step of preprocess image )
thanks
2 comentarios
Geoff Hayes
el 7 de Feb. de 2015
Editada: Geoff Hayes
el 7 de Feb. de 2015
gege - what do you consider the middle band of the above 4x4 matrix?
Ameligege
el 7 de Feb. de 2015
Respuesta aceptada
Más respuestas (2)
Stephen23
el 7 de Feb. de 2015
It depends on how you define "middle band": is this a fixed number of rows in the middle (e.g. always two rows in the middle, regardless of matrix size), or is this all rows excluding some fixed-width border (e.g. all rows except first and last row). These could both be described as "middle band", but have very different behaviors.
To change all rows except for the N first and N last rows, you can use this:
>> N = 1;
>> mat = ones(5);
>> mat(1+N:end-N,:) = 0
mat =
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
If you wish to have only a band in the center with a fixed number of rows, then you need to first specify how this should behave for both even and odd numbers of rows.
2 comentarios
Erik S.
el 7 de Feb. de 2015
Gege line, in you example picture the matrix is 8x8. In your code example the matrix, matt is 4x4. Can you say the index of rows and columns you consider the "middle band".
Stephen23
el 8 de Feb. de 2015
@gege line: "I need general solution for n*n". There are lots of nice people here who can probably help you create some MATLAB code for this. But we cannot read your mind: what is the "general solution"? We don't know if that middle band is a fixed width, or if it varies with the number of pixels, or follows some other rule, because you have not told us.
Erik S was nice enough to directly ask you for information that could help us understand this, which you did not answer.
Geoff Hayes
el 8 de Feb. de 2015
gege - your first step may be to create the mask (or whatever you want to call it) that you have shown in the attached image. It seems to follow a snake-like pattern, so you can use the attached function to generate that matrix of 0,1,2,3,...,m*n-1 where m is the number of rows in your matrix and n are the number of columns.
For example, if you run the function as
mask = generatemask(4,4);
then the mask becomes
mask =
0 1 5 6
2 4 7 12
3 8 11 13
9 10 14 15
Likewise, for your 8x8 example, the mask will be
mask = generatemask(8,8);
mask =
0 1 5 6 14 15 27 28
2 4 7 13 16 26 29 42
3 8 12 17 25 30 41 43
9 11 18 24 31 40 44 53
10 19 23 32 39 45 52 54
20 22 33 38 46 51 55 60
21 34 37 47 50 56 59 61
35 36 48 49 57 58 62 63
Now that you have the mask matrix, you can define your middle band as something between x and y. Again, for your 8x8 case, this would be all integers between (and including) 10 through 48. We can set these values to zero by
mask(mask >=10 & mask <=48) = 0;
mask(mask ~=0) = 1;
mask(1,1) = 1;
which gives us
mask =
1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 1
0 0 0 0 0 0 1 1
0 0 0 0 0 1 1 1
0 0 0 0 1 1 1 1
0 0 0 1 1 1 1 1
The question now becomes, why have 10 and 48 been chosen as the interval corresponding to the middle band of your matrix? Is this because the matrix is m==n==8 and so the middle band starts at mask(m/2+1,1) (which is 10) and ends at mask(m,n/2-1) which is 48? Using this logic, then the middle band of your 4x4 matrix would correspond to those values in the interval 3 and 9, which would mean that your 4x4 (without the middle band) would become
m = 4;
n = 4;
mask = generatemask(m,n);
mask(mask >=mask(m/2+1,1) & mask <=mask(m,n/2-1)) = 0;
mask(mask ~=0) = 1;
mask(1,1) = 1;
mask =
1 1 0 0
1 0 0 1
0 0 1 1
0 1 1 1
Is that the answer that you are looking for? Note that the above assumes that m and n are even, so you may have to add a ceil function call around the division by two
mask(mask >=mask(ceil(m/2)+1,1) & mask <=mask(m,ceil(n/2)-1)) = 0;
to handle the cases where m and/or n are odd. As for the more general mxn case, you may need to elaborate on what you expect (which others have requested that you do).
1 comentario
Ameligege
el 8 de Feb. de 2015
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
