To find the occurences

5 visualizaciones (últimos 30 días)
Mark Wood
Mark Wood el 23 de Mayo de 2021
Editada: DGM el 24 de Mayo de 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 comentario
Walter Roberson
Walter Roberson el 23 de Mayo de 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.

Iniciar sesión para comentar.

Respuesta aceptada

DGM
DGM el 23 de Mayo de 2021
Editada: DGM el 24 de Mayo de 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.

Más respuestas (0)

Categorías

Más información sobre Foundation and Custom Domains en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by