How to get the total count of non zero&non NaN elements by column

19 views (last 30 days)
Hi,
I have below Matrix, and I want to get the total count of "non zeor & non nan" elements in each column.
My Input:
1 0 nan 0 nan
2 0 1 0 nan
4 6 3 0 nan
I want the below output;
Output:
3 1 2 0 0
Kindly some one help

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 7 Oct 2016
Edited: Andrei Bobrov on 7 Oct 2016
a =[1 0 nan 0 nan
2 0 1 0 nan
4 6 3 0 nan]
out = sum(~isnan(a) & a ~= 0);
  2 Comments
Mekala balaji
Mekala balaji on 7 Oct 2016
Sir,
Thanks a lot, It works, is it applicable even if it is cell matrix?
Andrei Bobrov
Andrei Bobrov on 7 Oct 2016
a = {1 0 nan 0 nan
2 0 1 0 nan
4 6 3 0 nan};
out = sum(cellfun(@(a)~isnan(a) & a ~= 0,a));

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!