-2
$\begingroup$

I want to found how many elements in an array is not equal to zero. I use this code:

y=sum(img_dct~=0);
z=sum(y);

Can i make this with a different way? Is there a matlab instruction like 'numel' that can do the job?

$\endgroup$
1
  • 1
    $\begingroup$ Question is off-topic for DSP.SE - programming questions belong on stackoverflow.com. $\endgroup$
    – Paul R
    May 20, 2013 at 6:37

4 Answers 4

3
$\begingroup$

You can do it in one command by first using the colon operator to cast the matrix to a column vector.

z = sum(img_dict(:) ~= 0);
$\endgroup$
3
$\begingroup$

Prize for using numel?

z=numel(find(img_dct));
$\endgroup$
2
$\begingroup$

If you are using Matlab, there is a built-in command to do just this.

nnz(y)

$\endgroup$
1
$\begingroup$

You should get the number of how many elements are not equal to zero directly by

z=sum(img_dct~=0);
$\endgroup$
1
  • $\begingroup$ I take an array after this command and then i need to use again 'sum' to take the length of array. $\endgroup$
    – 20317
    May 19, 2013 at 18:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.