drop columns where there is a nan value in matlab

You can use the isnan function to find the indices of columns that have at least one NaN, and then use the (:, all(...)) syntax to drop those columns:

main.m
% create example data with NaN values
data = [1, 2, NaN, 4; NaN, 6, 7, 8; 9, 10, 11, 12];

% find the columns that contain NaN
nan_cols = any(isnan(data), 1);

% drop the NaN columns
data_no_nan = data(:, ~nan_cols);
217 chars
9 lines

This creates a new matrix data_no_nan that has the same number of rows as data, but without any columns that contain a NaN value.

related categories

gistlibby LogSnag