How can I remove NaN values from a matrix?
Ältere Kommentare anzeigen
My code so far is below. I have the code so that it skips the first 19 lines and starts at line 20. However, I need to remove the NaN values that are in my data like Columns = [10;0.04500;0;NaN;NaN] for example. The line I have to remove the NaN's runs, it's just not removing them. I'm not sure what isn't working. How do I fix my issue?
Thanks
fid = fopen('filename.txt');
Rows = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);
Columns= cellfun(@(x) textscan(x,'%f','delimiter','\t','CollectOutput',1) ...
, Rows{1,1}(20:end, :));
fid(isnan(fid(:,1)),:) = [];
Akzeptierte Antwort
Weitere Antworten (3)
Erik S.
am 18 Feb. 2015
Bearbeitet: per isakson
am 25 Mai 2018
You can you something like this
ind = ~isnan(Columns);
Columns=Columns(ind);
Cong Dong Ngoc Minh
am 16 Jun. 2020
0 Stimmen
You can try this way
idx =isnan(Columns)
Columns(idx,:) = []
berfin karabulut
am 14 Aug. 2020
0 Stimmen
or you can simply use "omitnan" function?
1 Kommentar
Walter Roberson
am 15 Aug. 2020
omitnan is not a Mathworks function. It is an option that can be used in some functions that are not relevant to the question asked.
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!