MATLAB: Mask making help – Divide a meshgrid in to ‘boxes’ and change the values of the cells in each ‘box’

image mask makingMATLABmatrix manipulation

Hi,
I am trying to divide up a meshgrid of zeros in to equal sized boxes, then turn the values of a particular box to equal 1. The idea behind this is to make a mask which can be place on top of another meshgrid so that boxes with zero values are 'not visible' and boxes with ones are 'visible'. The goal it to keep some central box 'on' and cycle through all the other boxes, turning them on and then off again individually. I have written some code which makes the mesh of zeros, 680 by 600 and then divides the mesh in to 68 by 60 boxes and can set the value of each box equal to 1, see below.
The issue I am having is that I cannot figure out a way of:
  • Firstly: adding the 1 values of a particular box back to the meshgrid i.e. box 2×3 corresponds to the mesh coordinates (21,31) to (30,40). So the Mesh would be all zeros apart from the cells the square outline by those coordinates.
  • Secondly: A way to set up the code with the features described in above but so that I could select the box I wanted to turn on at will, so for example – turn on the values in box 2×3- do something interesting- turn 2×3 back off- turn 3×3 on- ect…
Thank you for any help you can provide. I feel like I am close but just can pull the last bit together.
clear all; clc
x = 680;
y = 600;
Mesh = zeros(x , y);
[X,Y] = size(Mesh);
L = 10;
ki = 0;
kj = 0;
for i = 1 : L : X
ki = ki + 1;
for j = 1 : L : Y;
kj = kj + 1;
SubMesh = Mesh(i : (i + L - 1), j : (j + L - 1));
SomeOnes = ones( size(SubMesh) );
SomeOnesMesh = SubMesh * SomeOnes;
end
end
kii = ki
kjj = kj / kii

Best Answer

Some observations:
Your Mesh and SubMesh are always zero. Even if you multiply by SOmeONes, it's still zero.
Your convention of having rows (the vertical directions, dimension) be called x and the columns (the horizontal direction) be called y is contrary to the way the rest of the world does it and you might want to change that to be consistent with the rest of us and have your code be more maintainable.
You're not even using meshgrid() function so that confused me for a bit.
Beyond that I'm not really sure what you want to do. The code appears to create small arrays of zeros. It does what you tell it to do. Do you want a full size matrix where you have a patch of 1's in some certain box location?
Related Question