How can I remove NaN values from a 7862x6 cell?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, Matlab community:
I have a 7862x6 cell (variable name = A) that has NaN values at the end of some of the columns of data (hundreds of consecutive data points). How can I remove the NaNs so that I can find the true length of each column? I have looked through many current threads, but those solutions have not worked for my purpose. I think the problem is that I need a loop to work through the columns and rows, though I am not sure how to approach it. Any suggestions are appreciated!
- John
1 件のコメント
Vrajeshri Patel
2016 年 9 月 7 日
You can't remove them because it would change the size of the matrix.If you just want to find the length of the column (not including nan entries), here's an example:
k=[5 6 nan; 1 nan 3; 1 2 2];%example matrix
k(find(~isnan(k)))=1;%replace all non-nan numbers with 1s
s=nansum(k,1);%sum the values
採用された回答
dpb
2016 年 9 月 7 日
Presuming can decipher the actual storage you're trying to describe, here's a sample and solution to the number...
>> A=nan(10,3); for i=1:3,A(1:randi(8,1),i)=i;end % create some dummy data
>> A=num2cell(A)
A =
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [NaN] [ 3]
[ 1] [NaN] [NaN]
[ 1] [NaN] [NaN]
[NaN] [NaN] [NaN]
[NaN] [NaN] [NaN]
>> sum(isfinite(cell2mat(A))) % number non-NaN each column
ans =
8 5 6
>>
Since A is a cell array, you can remove those rows on a column-by-column basis if desired but isn't necessary to determine the sizes and may be more convenient if don't 'cuz can convert as-is to an 'ordinary' array which can make referencing and calculations simpler, depending on what else is needed.
2 件のコメント
dpb
2016 年 9 月 9 日
Actually, the "real" way would be
>> sum(cellfun(@isfinite,A))
ans =
8 5 6
>>
No need for the cell2mat which perhaps makes a copy. I've not tested performance...
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!