MATLAB: How to replace values in a cell array based on a condition

cell arrayconditionreplace

I have a cell array 'C' that is 1×525, with each cell holding a matrix with 18 columns and varying rows. I also have cell arrays 'F' and 'G', which are also 1×525, and contain matrices of size 1×18. What I want is: if element C{1,1}(10,14) is greater than F{1,1}(1,14), the element in C should be replaced with said value in F. If C{1,1}(10,14) is less than G{1,1}(1,14), it should be replaced with said value in G. I want to do this for every cell in C with its corresponding cell in F and G, with respect to only columns 8:18. Any ideas are welcome. Here is my code so far as to how I arrived at C, F, and G:
load weekdata.mat
N = size(weekdata,3);
C = cell(1,N); % weekly data
D = cell(1,N); % means
E = cell(1,N); % standard deviations
F = cell(1,N); % 3 z-scores above
G = cell(1,N); % 3 z-scores below
for k = 1:N
idx = weekdata(:,3,k)==0 & weekdata(:,4,k)==0 & weekdata(:,5,k)==1 & weekdata(:,6,k)==0;
tmp = weekdata(idx,:,k);
C{k} = tmp;
if size(C{1,k},1) > 1
D{1,k} = mean(C{1,k});
E{1,k} = std(C{1,k});
else
D{1,k} = C{1,k};
E{1,k} = zeros(1,18);
end
F{k} = D{k}+3*E{k};
G{k} = D{k}-3*E{k};
end

Best Answer

for k = 1:numel(C)
C{k}(:,8:18) = min(F{k}(:,8:18),max(G{k}(:,8:18),C{k}(:,8:18)));
end