remove negative and NaN from cell array.

3 ビュー (過去 30 日間)
Tha saliem
Tha saliem 2018 年 1 月 1 日
コメント済み: Tha saliem 2018 年 1 月 1 日
Hey
I have a cell array A
A= {NaN,-1,-1,0.8,-0.6,[],[]; NaN,NaN,0.9,1,NaN,-0.05,0.4; -1,NaN,NaN,0.7,0.2,NaN,0.79}
I want to remove negative and NaN values from all the rows and store result in a cell array like:
A{1,1}={0.8}
A{2,1}={0.9,1,0.4}
A{3,1}={0.7,0.2,0.79}
Thanks in advance.

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 1 日
NoNan = @(V) V(~isnan(V));
NewA = arrayfun(@(ROWIDX) NoNan([A{ROWIDX,:}]), (1:size(A,1)).', 'Uniform', 0);
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 1 月 1 日
I did not explicitly take empty into account, but the [A{ROWIDX,:}] will effectively discard empty matrices.
Tha saliem
Tha saliem 2018 年 1 月 1 日
Yes very efficient method. Thank You so much

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

その他の回答 (1 件)

KSSV
KSSV 2018 年 1 月 1 日
A= {NaN,-1,-1,0.8,-0.6,[],[]; NaN,NaN,0.9,1,NaN,-0.05,0.4; -1,NaN,NaN,0.7,0.2,NaN,0.79} ;
% Replace empty cells with NaN's
idx = cellfun('isempty',A) ;
A(idx) = {NaN} ;
% convert to matrix
B = cell2mat(A) ;
iwant = cell(size(B,1),1) ;
for i = 1:size(B,1)
T = B(i,:) ;
T = T(~isnan(T)) ;
iwant{i} = T(T>0) ;
end
  1 件のコメント
Tha saliem
Tha saliem 2018 年 1 月 1 日
Thank you so much for helping. This is also a good approach.

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by