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

2 次查看(过去 30 天)
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]

采纳的回答

Jan
Jan 2022-3-25
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

更多回答(2 个)

Voss
Voss 2022-3-25
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 个评论

请先登录,再进行评论。


Image Analyst
Image Analyst 2022-3-26
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by