I'm getting a math error in a script I'm writing and haven't been able to figure out the problem. I'm trying to use a loop to calculate the mean of a group of array elements from one specific index to another.
totalReps = totalReps(~cellfun('isempty', totalReps'));for i = 1:length(totalReps) A = cell2mat(totalReps(i)); nzer(end + 1) = length(find(A));endrepMeans = zeros(1, length(totalReps), 'single');for i = 1:length(totalReps) for j = int16(nzer(i) * 0.25):int16(nzer(i) * 0.75) repMeans(i) = repMeans(i) + totalReps{i,1}(1,j); end repMeans(i) = repMeans(i) / (nzer(i) * 0.5);end
totalReps is a N x 1 cell array composed of differently-sized 1 x N single-precision arrays. The purpose of this part of the script was to find the average of the middle 50% of the nonzero values in each element of totalReps. Every element of totalReps is some number of nonzero values followed by a number of zero values, so I used find() to create a parallel array nzer in order to index the nonzero elements. I then used a double for loop to add the middle 50% of each element into another parallel array repMeans. However after running the script, the elements of repMeans appear to all be off by around 1. If they are off by exactly 1 then it's easy enough to fix, but I'd like to understand what's going on.
I attached .xlsx files representing sample totalReps and corresponding repMean values.
Thanks in advance.
Best Answer