Is the find function a robust method to determine the number of specific values in a Matrix?

1 view (last 30 days)
Hello!
Sorry for the confusing Question-Formulation, but I could not think of a better one.
I have simulation in which values of a threedimensional matrix are changed over time. I have also some axes which plot the current number of specific values in this matrix, e.g. values which are ==1 or >4 for example. The way I implemented it, is the following
vec = find(A3(:,:,:)==1); % A3 is the threedimensional matrix
num = length(vec);
So I just found all the entries with value 1 and then just calculated the length of the vector, which contains the position of all this specific entries with value 1. As per my understanding, this should work like this, but I somehow get weird plots sometimes and I am not sure it its because of my simulation or of this finding-method. Is the length of this vector always equal to the number of specific entries in the matrix? Or does the vector sometimes has some trivial null-entries, that could fudge with the results?
I am sorry for the specific question. Thanks a lot guys!

Accepted Answer

Bob Thompson
Bob Thompson on 11 Oct 2019
That method will certainly work, and I would generally use the same concept. I would just use the sum command though to avoid the potential confusion with length.
num = sum(A3(:,:,:)==1);
The challenge with length is that it captures the longest dimension of a matrix, which may not capture the number of elements in an array.
I don't know that I have ever had find turn up extra results for a logic condition that simple, but I also haven't challenged it very robustly.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!