필터 지우기
필터 지우기

How can I remove NaN values from a 7862x6 cell?

조회 수: 2 (최근 30일)
John Harry
John Harry 2016년 9월 7일
댓글: dpb 2016년 9월 9일
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
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
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
John Harry
John Harry 2016년 9월 9일
Thank you very much, dpb! Your suggestion did exactly what I need!
dpb
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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by