Finding out number of columns with no NaNs
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I have a large dataset with 923 columns x 56 rows with some missing values (NaN) present. Ultimately I would like to find out the number of columns with no NaNs present in any of the 56 rows.
The data is in a structure flow.data and the column headers are in flow.textdata. I would like to create a new structure which only includes the columns without NaNs and has each column as a separate field with the corresponding column header as the fieldname.
I have written this bit of code but it's not really doing the job. It writes 923 fields in the new structure and in those fields it writes all of the original data (a 56x923 matrix). Could you please help me fix the code or suggest a better way to do things. Also, I was wondering how I can find out the number of fields in the new structure once I have created it successfully? I really appreciate the help!
varnames=flow.textdata(1:end);
for n=1:56 %number of rows
for i=1:923 %number of columns
for k=1:length(varnames);
if annual_flow(:,i) == NaN;
break
else annual_flow(:,i) ~= NaN %not equal to NaN;
new_structure.(varnames{k})(n,i)=annual_flow(n,i);
end
end
end
end
end
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 1 de Dic. de 2011
t = ~any(isnan(flow.data));
a2 = flow.data(:,t);
c2 = [flow.textdata(t);mat2cell(a2,size(a,1),ones(size(a2,2),1))];
new_struct = struct(c2{:});
3 comentarios
Más respuestas (2)
Jan
el 1 de Dic. de 2011
1. The comparison with NaN must be implemented as isnan, because by definition every direct comparison with NaN is false.
2. If you use a vector sized condition for if, Matlab inserts an all automatically. But you look for any occurrence. Then:
if any(isnan(annual_flow(:, i)))
3. The number of columns without NaNs:
Num = sum(not(any(isnan(annual_flow), 1)))
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!