Replacing a group of values in a matrix

19 views (last 30 days)
I am having some trouble with a problem I am trying to solve. I want to take the values in matrix M that = 1. When a value equals 1 in matrix M, I need to create an 8x8 square of 1's, replacing the zeros that surround that value of 1. A shortened example is listed, the data set I am working with is much larger.
For example, my table would look like this with a few ones scattered about:
But I would like to transform those areas around the 1's to look like this:
My thinking is to write a for loop that replaces the area around the singular 1 with a matrix of ones in an 8x8 fashion (like in the second figure). So I would be replacing the area of matrix M with matrix a but I am not sure if this is possible or there is a better way to replace these values for every appearance of a 1 in my matrix. I've been googling for hours but not familiar with matlab enough to know how to work this out. Any steps in the right direction would be appreciated, thank you.
M = zeros(100,100);
M(8,8) = 1; % example data point
M(15,22) = 1; % example data point
a = ones(8,8)

Accepted Answer

Matt J
Matt J on 25 Jun 2021
If you don't have the Image Processing Toolbox, you can also do,
M=conv2(M,ones(8),'same');
  2 Comments
Evan Anderson
Evan Anderson on 25 Jun 2021
This is also very helpful and seems a bit easier to work with. Having trouble creating a new matrix for each individual (x,y) coordinate but I am working through it at the moment. Thank you!!
Evan Anderson
Evan Anderson on 25 Jun 2021
Got it to work, here is the for loop I made that gets me the right output, thank you for the help! Obviously, without the values of the variables it is a bit confusing but wanted to show that it helped me!
for l = 1:493
M(l) = mat2cell(zeros(232,136),232,136);
M{l}(z(l,1),z(l,2)) = 1;
M{l} = conv2(M{l},ones(8),'same');
end

Sign in to comment.

More Answers (2)

Matt J
Matt J on 24 Jun 2021
Edited: Matt J on 24 Jun 2021
M=imdilate(M,ones(8))

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 25 Jun 2021
You can start with a logical indexing approach to locate where 1's located, e.g.:
[Rn, Cn] = find(AM); % Initial Matrix with some 1s and many 0s
%% E.g.
A=zeros(8);
A(3,3)=1;
[Rn,Cn] = find(A==1);
A([(Rn-2):(Rn+2)], [(Cn-2):(Cn+2)])=1; % Creates 5-by-5 1s in A.
  2 Comments
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 25 Jun 2021
To make this run within a loop, you'd need to care of special cases when the index values (Rn and Cn) residing along the corner or edge or closer to the edges of the given matrix. That would create a problem with indexes end up being outside of the given matrix or negative indexes.
Evan Anderson
Evan Anderson on 25 Jun 2021
Gotcha, that works to replace and I think I can do the for loop from there. I just couldn't figure out how to replace values. I really appreciate that guidance, it was just the bump I needed

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!