フィルターのクリア

find a Nan in column of cell array and delete the row

3 ビュー (過去 30 日間)
Alex
Alex 2013 年 12 月 5 日
コメント済み: sixwwwwww 2013 年 12 月 5 日
Hi everyone, I have a cell array (1152,4) In this cell array I would like see if in each column 1,2,3,4 if there is a Nan, if there is a Nan I would like delete the row.
For that I try to use : cellfun(@isnan,newTab,'UniformOutput',false) and it sends a matrix with logical value (0/1), how can I delete row if there is a 1 in my matrix?
Thank you

回答 (5 件)

sixwwwwww
sixwwwwww 2013 年 12 月 5 日
編集済み: sixwwwwww 2013 年 12 月 5 日
here is an example which can give you idea how you can do it:
a = rand(1152, 4);
a(randi(1152, 1, 20), :) = NaN;
a = num2cell(a);
b = cellfun(@isnan, a);
idx = find(b(:,1));
for i = 2:size(a, 2)
idx = union(idx, find(b(:,i)));
end
a(idx, :) = [];

Alex
Alex 2013 年 12 月 5 日
編集済み: Alex 2013 年 12 月 5 日
thank you, just why you start the loop to i=2 and not i=1?
  1 件のコメント
sixwwwwww
sixwwwwww 2013 年 12 月 5 日
because of this:
idx = find(b(:,1)); and also we need to use intersect afterwards which need to arrays so i calculated the first and then started the loop from 2

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


Alex
Alex 2013 年 12 月 5 日
編集済み: Alex 2013 年 12 月 5 日
and why the loop is used ? If I try only:
b = cellfun(@isnan, a);
idx = find(b(:,1));
a(idx, :) = [];
It works?!?
  1 件のコメント
sixwwwwww
sixwwwwww 2013 年 12 月 5 日
no it will not work because if NaN will appear in 2nd or 3rd or 4th column then that row will not be deleted because we are not getting indices for those rows. I edited my answer which has small mistake. So you can accept my edited question. I correct it

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


Jos (10584)
Jos (10584) 2013 年 12 月 5 日
If your cell array C has only numbers or NaNs, this will do the job
C = {1 2 3 4 ; 11 12 NaN 14 ; 21 22 23 24}
C(any(isnan(cell2mat(C)),2),:) = []
If your cell array C has a mixture of numbers, char arrays or NaNs, the one-liner above will not work, but this will:
C = {1 2 3 4 ; 'DeleteThisRow' 12 NaN ones(3) ; 21 rand(2) 23 24:29}
C(any(cellfun(@(x) numel(x)==1 & isnumeric(x) && isnan(x),C),2),:) = []

Alex
Alex 2013 年 12 月 5 日
thank you but my real probleme is that I got several table:
infrastructure.capteur(1,1).tableau %1st tab
infrastructure.capteur(2,1).tableau %2nd tab
... there a 8 tab
When there is a Nan is a row, i need to delete each rows in each tabs
  1 件のコメント
sixwwwwww
sixwwwwww 2013 年 12 月 5 日
then you can use for loop to do it. Access data from each tab in a big cell array and then check each cell array and delete the rows which contain NaN

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

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by