how to replace NaN values with zero?

261 visualizaciones (últimos 30 días)
Puspa patra
Puspa patra el 31 de Dic. de 2018
Editada: hxen el 29 de Nov. de 2023
Hello,
I have a matrix of 361*181 dimention, which contain NaN and some values.
I want to replace those NaN values with 0, I have tried A(isnan(A))=0; command but is showing error.
the error read as " Subscript indices must either be real positive integers or logicals".
can anyone help me out this?
  2 comentarios
Walter Roberson
Walter Roberson el 31 de Dic. de 2018
What shows up for
which -all isnan
Puspa patra
Puspa patra el 31 de Dic. de 2018
Thanks

Iniciar sesión para comentar.

Respuesta aceptada

madhan ravi
madhan ravi el 31 de Dic. de 2018
Editada: madhan ravi el 31 de Dic. de 2018
Might happen that your isnan is somehow a variable maybe try the following:
clear all
A(isnan(A))=0;
%%%% an example
A=rand(3);
A([2 3],2)=NaN;
A(isnan(A))=0;
Gives:
A =
0.7339 0.8422 0.1934
0.0039 0 0.3316
0.1183 0 0.6213
  12 comentarios
Ashishkumar Gupta
Ashishkumar Gupta el 5 de En. de 2023
Tried ur code for my problem (https://de.mathworks.com/matlabcentral/answers/1889142-how-to-remove-nans-from-struct) but did not work out!!
I want to replace all the NaN with 0 so that size is same for future use.
hxen
hxen el 29 de Nov. de 2023
Editada: hxen el 29 de Nov. de 2023
Ashishkumar Gupta The png attachments in your linked post are not informative. It might be you are not accessing the structure data correclty to get the value you wish to set to zero.
For example, you have a struct named WS_struct with several fields. To access the values (and assuming they are numeric), you would type: WS_struct.HP_EDU_00571_eTA
Just be sure that the fields give numeric data and you correctly access the data. The data fields in the other pic are double, so the method outlined about should work and you can explore with this code how to manipulate the struct fields and set to new named varibles and access entries:
%% example code to set all nan entries to zero after accessing from a struc file
clear all; clc;
M = 5*rand(5,5); % make a 5x5 matrix with random nums on [0,5]
n = randi([1 3],1); % rand num from 1 to 3 to use as the num of nans
randRows = randi([1 5],n,1); % get n rand rows from the 5 cols
randCols = randi([1 5],1,n); % get n rand cols from the 5 cols
fprintf('\nA random 5x5 matrix with 1 to 9 random nan entries:\n');
M(randRows,randCols) = nan
% Now create a structure with field randM and set to M
S.randnum = n;
S. randRows = randRows
S. randCols = randCols;
S.randM = M;
save('randomMatrix.mat','S'); % save structure S
% clear all memory and access data directly from struct S
clear all; % mimicking someone handing you the structure for the first time
load randomMatrix.mat
% set matrix newM to S.randM
newM = S.randM;
fprintf('\nSetting all nan entries to zero:\n');
newM(isnan(newM)) = 0 % find nan entries and set to zero

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by