Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

want to calculate the sum of the numbers appear in an array that is mix of NaNs and numbers

Asked by hdr mhr on 1 May 2013

I have an array that has the following pattern, lets call it A

A=[NaN NaN...NaN 0 1.445 4.223 5.353 1.3534 NaN NaN...NaN 2.45 1.54 0.98 ....NaN....]
What I want to do is to calculate the sum of each group of numbers that appear after the each group of NaNs, for example sum(0 1.445 4.223 5.353 1.3534) = B(1), sum(2.45 1.54 0.98)= B(2), and so on.

Can you please guide me through an efficient way to do this?

0 Comments

hdr mhr

Products

2 Answers

Answer by Cedric Wannaz on 1 May 2013
Edited by Cedric Wannaz on 1 May 2013
Accepted answer

It is not trivial actually. Look at the following and let me know if there is anything unclear. Also, I let you check that it manages well limit cases, and update it otherwise.

 isnanId = isnan(A) ;
 groupId = [1, isnanId(1:end-1)] ;
 groupId = cumsum(groupId(~isnanId)) ;
 A = A(~isnanId) ;
 B = accumarray(groupId(:), A(:)) ;

Running this code produces:

 >> B
 B =
   12.3744
    4.9700

which is the group-sum that you are looking for.

1 Comment

hdr mhr on 2 May 2013

It worked as it supposed to be, thanks a lot.

Cedric Wannaz
Answer by Azzi Abdelmalek on 1 May 2013
A=[NaN NaN NaN 1 2 3 NaN NaN NaN 4 5 6 10 NaN NaN 2 NaN NaN 1 2 0 4]
idx=find(~isnan(A))
id=[1 diff(idx)]
ii=[1 find(id>1)]
for k=1:numel(ii)-1
  s(k)=sum(A(idx(ii(k)):idx(ii(k+1)-1)))
end
s(k+1)=sum(A(idx(ii(k+1)):idx(end)))

1 Comment

hdr mhr on 2 May 2013

Thank you for the answer. It works perfectly fine but I think the first algorithm works a bit faster when the array is large. For a 736*1 array, the first method took:

Elapsed time is 2.634142 seconds.

and your method:

Elapsed time is 2.979654 seconds.

Thank you very much

Azzi Abdelmalek

Contact us