フィルターのクリア

how can I get the max of the values between NaN's in an array

5 ビュー (過去 30 日間)
Joanie
Joanie 2014 年 6 月 10 日
コメント済み: Joanie 2014 年 6 月 10 日
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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 10 日
編集済み: Azzi Abdelmalek 2014 年 6 月 10 日
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)
  2 件のコメント
Matt J
Matt J 2014 年 6 月 10 日
I don't get the correct answer from this. I get,
15 13 11 9
Joanie
Joanie 2014 年 6 月 10 日
That's because Azzi added 4 5 at the end of T :)

サインインしてコメントする。

その他の回答 (2 件)

Matt J
Matt J 2014 年 6 月 10 日
編集済み: Matt J 2014 年 6 月 10 日
T=sum(T,2);
lims=reshape(find(isnan(T)),2,[]);
n=size(lims,2);
result=nan(1,n);
for i=1:n
result(i)=max(T(lims(1,i)+1:lims(2,i)-1));
end
  3 件のコメント
Matt J
Matt J 2014 年 6 月 10 日
I don't fully understand your original code, but here is how I would extract each individual block of T
if ~isnan(T(1))
T=[nan,nan;T];
end
if ~isnan(T(end))
T=[T;nan,nan];
end
nanloc=find(isnan(T(:,1)));
for i=1:length(nanloc)-1
a=nanloc(i);
b=nanloc(i+1);
if b-a==1,
continue;
else
a=a+1;b=b-1;
end
Tblock = T(a:b,:), %current block
end
Joanie
Joanie 2014 年 6 月 10 日
Thank you I can work with this!

サインインしてコメントする。


Andrei Bobrov
Andrei Bobrov 2014 年 6 月 10 日
l = ~isnan(T(:,1));
k = [l(1);diff(l)] == 1;
ii = cumsum(k);
z = (1:size(T,1))';
out = accumarray(ii(l),z(l),[],@(x)max(sum(T(x,:),2)));

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by