Clear Filters
Clear Filters

Add a number to a region of cells bounded by nonzero numbers in a matrix

2 views (last 30 days)
If I have for example, A matrix of any size, with data surrounded by zeros.
Notice that within the region of interest, the area bounded by data there are some zeros within. However, the surrounding zeros are not useful data (zero strain from the background, zero strain within the sample). However I want to + 0.1 to the entire region of numerical (nonzero) data, which is my sample.
I have tried to + 0.1 by using the nonzero function for matrixes, but have noticed the zeros within the region of interest gets left behind so I was hoping to try and define the region boundaries by taking the min and max horizontal value, and vertical value, and then + 0.1 on this region. It would be even better if I could identify the pockets of zeros surrounded by nonzero numbers, and add + 0.1 to this.
A = [0 0 0 0 0 0 0 0 0 0;
0 0 0 0 3 9 2 1 3 0;
0 0 2 4 1 0 0 0 0 0;
0 0 3 3 2 4 9 2 0 0;
0 5 1 3 0 0 4 2 0 0;
0 0 2 1 3 0 0 4 8 0;
0 2 1 2 3 3 1 3 7 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0]
B = A;
isNonzero = A~=0;
B(isNonzero) = B(isNonzero)+0.1;
% This adds +0.1 to the non zero values but does not consider the zeros
% encapsulated by the other numbers
I would hope to get something like this:
B = [0 0 0 0 0 0 0 0 0 0;
0 0 0 0 3.1 9.1 2.1 1.1 3.1 0;
0 0 2.1 4.1 1.1 0 0 0 0 0;
0 0 3.1 3.1 2.1 4.1 9.1 2.1 0 0;
0 5.1 1.1 3.1 0.1 0.1 4.1 2.1 0 0;
0 0 2.1 1.1 3.1 0.1 0.1 4.1 8.1 0;
0 2.1 1.1 2.1 3.1 3.1 1.1 3.1 7.1 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0]

Accepted Answer

Jan
Jan on 25 Mar 2022
A = [0 0 0 0 0 0 0 0 0 0;
0 0 0 0 3 9 2 1 3 0;
0 0 2 4 1 0 0 0 0 0;
0 0 3 3 2 4 9 2 0 0;
0 5 1 3 0 0 4 2 0 0;
0 0 2 1 3 0 0 4 8 0;
0 2 1 2 3 3 1 3 7 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0];
M = (A ~= 0);
M = cumsum(M, 1, 'forward') & cumsum(M, 1, 'reverse') & ...
cumsum(M, 2, 'forward') & cumsum(M, 2, 'reverse');
A(M) = A(M) + 0.1
A = 9×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.1000 9.1000 2.1000 1.1000 3.1000 0 0 0 2.1000 4.1000 1.1000 0 0 0 0 0 0 0 3.1000 3.1000 2.1000 4.1000 9.1000 2.1000 0 0 0 5.1000 1.1000 3.1000 0.1000 0.1000 4.1000 2.1000 0 0 0 0 2.1000 1.1000 3.1000 0.1000 0.1000 4.1000 8.1000 0 0 2.1000 1.1000 2.1000 3.1000 3.1000 1.1000 3.1000 7.1000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

More Answers (2)

Voss
Voss on 25 Mar 2022
Here's one way:
A = [0 0 0 0 0 0 0 0 0 0;
0 0 0 0 3 9 2 1 3 0;
0 0 2 4 1 0 0 0 0 0;
0 0 3 3 2 4 9 2 0 0;
0 5 1 3 0 0 4 2 0 0;
0 0 2 1 3 0 0 4 8 0;
0 2 1 2 3 3 1 3 7 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0];
isNonzero = A~=0;
idx = cummax(isNonzero,1) ...
& cummax(isNonzero,1,'reverse') ...
& cummax(isNonzero,2) ...
& cummax(isNonzero,2,'reverse')
idx = 9×10 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
A(idx) = A(idx)+0.1
A = 9×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.1000 9.1000 2.1000 1.1000 3.1000 0 0 0 2.1000 4.1000 1.1000 0 0 0 0 0 0 0 3.1000 3.1000 2.1000 4.1000 9.1000 2.1000 0 0 0 5.1000 1.1000 3.1000 0.1000 0.1000 4.1000 2.1000 0 0 0 0 2.1000 1.1000 3.1000 0.1000 0.1000 4.1000 8.1000 0 0 2.1000 1.1000 2.1000 3.1000 3.1000 1.1000 3.1000 7.1000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  4 Comments

Sign in to comment.


Image Analyst
Image Analyst on 26 Mar 2022
Looks like no one on this thread is familiar with the functions in the Image Processing Toolbox that make this operation trivial, and a lot simpler than the other code.
A = [0 0 0 0 0 0 0 0 0 0;
0 0 0 0 3 9 2 1 3 0;
0 0 2 4 1 0 0 0 0 0;
0 0 3 3 2 4 9 2 0 0;
0 5 1 3 0 0 4 2 0 0;
0 0 2 1 3 0 0 4 8 0;
0 2 1 2 3 3 1 3 7 0;
0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0];
% Find non-zeros and fill "pockets/holes" of zeros.
mask = imfill(A ~= 0, 'holes')
mask = 9×10 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
% Add 0.1 everywhere that the mask is true:
A(mask) = A(mask) + 0.1
A = 9×10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 3.1000 9.1000 2.1000 1.1000 3.1000 0 0 0 2.1000 4.1000 1.1000 0 0 0 0 0 0 0 3.1000 3.1000 2.1000 4.1000 9.1000 2.1000 0 0 0 5.1000 1.1000 3.1000 0.1000 0.1000 4.1000 2.1000 0 0 0 0 2.1000 1.1000 3.1000 0.1000 0.1000 4.1000 8.1000 0 0 2.1000 1.1000 2.1000 3.1000 3.1000 1.1000 3.1000 7.1000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Community Treasure Hunt

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

Start Hunting!