how i can make change on middle band of specific matrix?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2015-2-7
编辑:Geoff Hayes
2015-2-7
gege - what do you consider the middle band of the above 4x4 matrix?
采纳的回答
Star Strider
2015-2-8
This is my best (and admittedly exhausting) effort:
colormap(gray(3))
matsz = 8;
X = ones(matsz);
for k1 = 1:2*matsz
dg = matsz-k1;
bands = [6 10];
clr = [0*(k1<=bands(1)) + 1*((k1>bands(1)) & (k1<=bands(2))) + 2*(k1>bands(2))];
xd = diag(X,dg) * clr;
X = diag(xd,dg) + X;
end
figure(1)
surf(X)
view([0 90])
It will generalise with difficulty to different size matrices, but it works with this one. The heuristic part of this is getting the ‘bands’ vector limits correct. It took me a while to get them working correctly. There may be better ways to calculate your matrix, but this has the virtue of working.
It produces this plot:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173354/image.png)
2 个评论
Star Strider
2015-2-8
My pleasure!
I’ve been thinking about it since you posted it. I knew the diag function was the solution, though only this morning figured out how to make it work. The ‘breakthrough’ was realising that specifying a specific off-diagonal creates a matrix equal in size to the original matrix but with the specified diagonal, so I then only needed to assign the correct colour to each of the diagonals and add them. Deciding how to code the colouring (the ‘bands’ vector) was actually the more difficult part of the solution.
更多回答(2 个)
Stephen23
2015-2-7
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 个评论
Erik S.
2015-2-7
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
2015-2-8
@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
2015-2-8
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!