Counting the number of elements in matlab?

You can get the number of elements with numel(x) You can get the number of non-zeros with sum(x ~= 0) So the ratio is one divided by the other.

You can get the number of elements with numel(x). You can get the number of non-zeros with sum(x ~= 0). So the ratio is one divided by the other.

Oli: Thanks for your quick response, I am using the all(x==1) to check whether vector x has all non zero elements or not. But even if I have a vector like x = 0 0 0 0 0 0 , all (x==1) gives me a TRUE value, can you tell me why it does so? – user537670 Apr 29 at 8:57 @user: That gives me false, not true.

– Oli Charlesworth Apr 29 at 9:04 Ohh...sorry my bad...suppose I wanted to check whether all my elements are zero will all(x==0) would work? – user537670 Apr 29 at 9:10 @user: Yes, that would work. Alternatively, ~any(x) would work.

– Oli Charlesworth Apr 29 at 9:13 @Oli: Thanks a lot. – user537670 Apr 29 at 9:14.

The ratio of non-zero elements to all elements in a vector is: r = length(find(x)) / length(x) What length does is kind of obvious. Find gives you the index of all non-zero elements. Edit: Fixed mistake of using size instead of length.

No, because size doesn't give you a scalar! – Oli Charlesworth Apr 29 at 8:50 @Oli-Charlesworth Ah you're right, my mistake, fixed now. – Darhuuk Apr 29 at 9:01 @Darhuuk: Removed -1.

But numel is a more general solution than even length (consider what happens if x is a matrix, not a vector). – Oli Charlesworth Apr 29 at 9:02 @Oli-Charlesworth Good point, I was thinking the same when you typed your comment and already updated my answer to clarify. – Darhuuk Apr 29 at 9:03 Your ratio is upside down.

– mtrw Apr 29 at 9:04.

The number you are searching for is 20,000.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions