MATLAB: Replacing nans and greater than

greater thannan

I have a file with 3 columns. I need to change all the NaNs in the second column of the cell array to empty cells, and the corresponding row in column 3 with number 4. I also need to do the same for numbers greater than 0.2, changing row 3 to a 5.
I tried something like this:
if database(:,2) = is a nan then it will change to []
database(:,3) = 4
if database(:,2) > 0.2
database(:,3) = 5

Best Answer

Nicole - the easiest (but not necessarily most efficient) way is to just iterate over each element of the second column and apply your conditions, using the isnan function. Something like
numRows = size(database,1);
for k=1:numRows
if isnan(database{k,2})
database{k,2} = [];
database{k,3} = 4;
elseif database{k,2} > 0.2
database{k,3} = 5;
end
end
Note how the {} braces are used to access (get and set) the elements in the cell array.