フィルターのクリア

removing entire row in cell array if first column is a NaN

3 ビュー (過去 30 日間)
David C
David C 2012 年 6 月 6 日
My data matrix (after reading into matlab with xlsread) contains many rows of string and numerical data, and at the end, there sometimes exists several rows of "NaN"s
I want to delete these specific rows but can't seem to do it properly.
I've tried q=cellfun(@(x) all(isnan(x)),myMatrix); myMatrix(q); but the result is no longer a cell
I've tried findNan=isnan(myMatrix(:,1)) to find the index of the row containing the NaN's to kill it, but it gives an error: ??? Undefined function or method 'isnan' for input arguments of type 'cell'.
Any assistance is appreciated! Thanks :)
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 6 日
xlsread() can return 3 matrices, one for numeric, one for text, one for raw. Which one are you working with? To get the mix of numeric and text you must be working with raw ?
David C
David C 2012 年 6 月 6 日
i'm working with the RAW matrix ... this is a must for me at the moment

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 6 月 6 日
myMatrix( cellfun( @(C) isnumeric(C) && isnan(C), myMatrix(:,1) ), :) = [];
  3 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 6 日
You have cells that contain strings; strings fail the isnumeric() test, and the && operator does not go on to the second test if the first test fails. isnan() is thus only applied to numeric values, and you only have one numeric value per cell so isnan() will return a scalar value, so you do not need any(). If you left off the isnumeric() test, then isnan() would be applied to the strings; it is well defined on strings but it returns one value per character.
An alternate coding should be
myMatrix( cellfun( @(C) any(isnan(C)), myMatrix(:,1) ), :) = [];
On the other hand, if somehow the matrix could contain other data types in the cells, then the isnumeric() version would be safer. Safer yet would be
myMatrix( cellfun( @(C) isnumeric(C) && any(isnan(C(:))), myMatrix(:,1) ), :) = [];
which allows for the possibility that a numeric array might have slipped in
David C
David C 2012 年 6 月 6 日
super!! My data is consistent, and in actuality, I only have to search down the first col of my cell array which is an array of strings (which is what this code is actually doing correct? the myMatrix(:,1) part of it?)
then it searches that array and performs the isnumeric and isnan functions on it till it gets a true "1" value, and that row's index is returned by cellfun, which in turn is made into a blank [] am I understanding that correctly?
If so, since I know my first column in the cell array will always be strings, I technically don't need the isnumeric(C) part of it right?
Thanks again!

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

その他の回答 (1 件)

the cyclist
the cyclist 2012 年 6 月 6 日
Does this do what you want?
% Some made-up data
c = num2cell([1 1; 2 2; NaN NaN]);
% Find the rows with all NaN
indexToRowToDrop = all(arrayfun(@(x)isnan(x{:}),c),2)
% Drop the bad rows
c(indexToRowToDrop,:) = [];

カテゴリ

Help Center および File ExchangeEmbedded Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by