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]
1 个评论
Mathieu NOE
2022-3-25
hello
maybe the start of a solution :
and do the correction of the data inside the boundary
采纳的回答
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
更多回答(2 个)
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')
A(idx) = A(idx)+0.1
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')
% Add 0.1 everywhere that the mask is true:
A(mask) = A(mask) + 0.1
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!