How to replace values in a cell array based on a condition?
Show older comments
I have a cell array 'C' that is 1x525, with each cell holding a matrix with 18 columns and varying rows. I also have cell arrays 'F' and 'G', which are also 1x525, and contain matrices of size 1x18. 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
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!