To find the occurences

2 views (last 30 days)
Mark Wood
Mark Wood on 23 May 2021
Edited: DGM on 24 May 2021
How to find the occurences,
For example: For the given matrix, our threshold conditon is given as 4
We need to find the number of coherent elements of each element present in the matrix (In this example there are only 3 elements ie. 0,1,2)
Now for element 0, we need to find number of connecting 0's chain whose length is equal to or greater than out threshold given value.
Now, as we have 2 chains of connecting 0's, So, the number of coherent elements of 0 is 8(which is count of elements in chain)
Now, Similarily for element 1,
Now, here as we have only 1 chain of connecting 1's, So the number of coherent elements of 1 is 8
Now, finally for element 2,
Now, in this we have 2 chain of connecting 2's, But the number of coherent elements of 2 will be 6 as the count of elements in left chain is less than our threshold value.
At last display all the number of elements with their respective number of chains and number of coherent elements.
So our final answer should become this,
***********************************************************************************************
NOTE : If there is only 1 occurence of an element, consider that also a seperate chain.
For eg, If there is a single element 3 in the above matrix, then we will have 1 chain(only single element in that chain) of connecting 3's, But the number of Number of coherent elements of 3 will be 0 as this do not satisfy our threshold value condition.
************************************************************************************************
Please help with the MATLAB code of this approach.
  1 Comment
Walter Roberson
Walter Roberson on 23 May 2021
See regionprops() and bwareafilt()
... for any one value, finding the matching chains can be done as a single expression. Doing everything in single expression would require using a helper anonymous function.

Sign in to comment.

Accepted Answer

DGM
DGM on 23 May 2021
Edited: DGM on 24 May 2021
You could use image processing tools easily enough: Since this appears to be using 8-connectivity:
A = [1 1 1 1 1;
2 1 1 2 2;
2 0 2 1 0;
2 0 2 2 0;
0 0 2 0 0];
threshold = 4;
U = unique(A); % find unique elements of A
objectcount = zeros(numel(U),1);
totalarea = zeros(numel(U),1);
for c = 1:numel(U)
S = regionprops(A==U(c),'area');
area = [S.Area];
objectcount(c) = numel(area); % obj count before truncation
totalarea(c) = sum(area(area>=threshold));
end
% display results
table(U,objectcount,totalarea)
ans = 3×3 table
U objectcount totalarea _ ___________ _________ 0 2 8 1 1 8 2 2 6
EDIT: I adapted this to be a bit more generalized, automatically finding unique elements and building masks as needed.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!