MATLAB: How can I get the max of the values between NaN’s in an array

arraynan

I have an array like this one:
T = [NaN NaN; ...
1 3;
2 4;
5 6;
8 7;
NaN NaN;
NaN NaN;
4 5;
6 7;
NaN NaN;
NaN NaN;
1 2;
2 4;
5 6;
NaN NaN];
Now I'd like to add the columns of the values between the Nan's and get the max values of the sums. So the outcome in this case would be:
max = [15; 13; 11]
I tried this:
NaNloc=isnan(T);
ind=find(NaNloc(:,1));
for i=1:(length(ind)-1)
if ind(i+1)-ind(i) >1
patloc=[ind(i) ind((i+1))];
patarray=patloc(1):1:patloc(2);
for j=patarray(1):patarray(length(patarray))
a=T(j,:);
f=a(1)+a(2);
fsave(j-patloc(1)+1,:)=f;
end
max(fsave)
end
end
but I get the outcome: 15 15 11

Best Answer

T = [NaN NaN; 1 3; 2 4; 5 6; 8 7; NaN NaN; NaN NaN; 4 5; 6 7; NaN NaN; NaN NaN; 1 2; 2 4; 5 6; NaN NaN;4 5];
h=[1;isnan(T(:,1)) ;1]';
out=arrayfun(@(x,y) max(sum(T(x:y,:),2)),strfind(h,[1 0]),strfind(h,[0 1])-1)