Finding zero's groups that are bounded in a matrix and there amounts

Hi, i would to find "islands of zeros" in a matrix. for example, if i have this matrix i will get a vector that says [4] because there is just one group of bounded zeros and it is 4 elements. The size of the vector is the number of groups.
1 2 3 4 5
1 1 0 1 2
7 0 0 0 5
1 5 6 7 8
Is there an efficient way to do it?(build in functions).
thanks

Risposte (1)

A = [
1 2 3 4 5
1 1 0 1 2
7 0 0 0 5
1 5 6 7 8
4 0 2 0 0
5 1 3 8 2
]
A = 6×5
1 2 3 4 5 1 1 0 1 2 7 0 0 0 5 1 5 6 7 8 4 0 2 0 0 5 1 3 8 2
p = regionprops(~A, 'area');
results = [p.Area]
results = 1×3
4 1 2

6 Commenti

is there also a way to detect islands of variables that are smaller then a threshold and not exactly zero?
thresh = 2;
A = [
1 2 3 4 5
1 1 0 1 2
7 0 0 0 5
1 5 6 7 8
4 0 2 0 3
0 1 3 8 2
]
A = 6×5
1 2 3 4 5 1 1 0 1 2 7 0 0 0 5 1 5 6 7 8 4 0 2 0 3 0 1 3 8 2
BW = ~A;
BWfilt = bwareafilt(BW, [1 thresh]);
p = regionprops(BWfilt, 'area');
results = [p.Area]
results = 1×2
2 1
BWfilt
BWfilt = 6×5 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0
I don't think we talk about the same thing with threshold.
for input
1 2 3 4 5
1 1 6 1 2
7 0 6 0 5
7 5 6 7 8
and threshold 2
i accept to get all the islands that are smaller then 2:
Therefore: [4,2].
Thanks!
If you want islands that are smaller than 2, then why are you expecting a 4 outcome?
Does the "2" refer to the number of islands you want? As in you want the two largest islands?
2 is the threshold. there are two islands of numbers (not only zero) that are smaller then 2. One is sized 4 (1,1,1,0). And the other one is 2 (0,0)
A = [
1 2 3 4 5
1 1 6 1 2
7 0 6 0 5
7 5 6 7 8]
A = 4×5
1 2 3 4 5 1 1 6 1 2 7 0 6 0 5 7 5 6 7 8
threshold = 2;
BWfilt = A <= threshold & A ~= 0
BWfilt = 4×5 logical array
1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0
p = regionprops(BWfilt, 'area')
p = 2×1 struct array with fields:
Area
[p.Area]
ans = 1×2
4 2

Accedi per commentare.

Categorie

Richiesto:

il 16 Dic 2021

Commentato:

il 21 Dic 2021

Community Treasure Hunt

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

Start Hunting!

Translated by