How to remove NaN in rows and columns of excel data set in matlab?

조회 수: 8 (최근 30일)
Haritha
Haritha 2018년 9월 19일
댓글: Image Analyst 2020년 9월 29일
Hi, I have the excel dataset of 50*48 matrix but it is having some NaN values also? I want to remove the NaN values from the matrix without loss of data.
Thank you in advance.
  댓글 수: 2
jonas
jonas 2018년 9월 19일
편집: jonas 2018년 9월 19일
An array must be rectangular and cannot have truly empty elements. Do you want to replace them by something? How do you envision your desired output?
Haritha
Haritha 2018년 9월 19일
I want to remove this NaN value

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2018년 9월 19일
You can use isnan() to identify nan locations and then build a cell array with nulls there. If m is your matrix (untested code):
[rows, columns] = size(m);
nanLocations = isnan(m);
ca = cell(rows, columns); % Initialize cell array.
for row = 1 : rows
for col = 1 : columns
if ~nanLocations(row, col)
ca{row, col} = m(row, col); % Only fill cell if it's not nan.
end
end
end
xlswrite(filename, ca);
  댓글 수: 3
Bryan Tan
Bryan Tan 2020년 9월 29일
Hello Image Analyst, is there a way to make every rows after NaN as row 1 and the average all the row 1?
I have 3 rows of NaN, but I have numbers after that. So after every 3rd NaN, I want to make it as row 1, so I can average each row 1.
For example the first data has no NaN before it:
1
2
3
NaN
NaN
NaN
4
5
6
10
NaN
NaN
NaN
7
8
9
So the output average should be:
4
5
6
10
Thank you. My discussion topic is here:
https://au.mathworks.com/matlabcentral/answers/601879-averaging-and-interpolating-random-number-of-rows-of-data
Image Analyst
Image Analyst 2020년 9월 29일
Yes, try this:
badRows = isnan(ca{:, 1}); % Find nans in column 1.
ca = ca(~badRows, :); % Extract all rows except the bad rows.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by