MATLAB: How to add columns with a certain formula

column formula calculation

Hi guys, I have a cell array with 90 cells. Each cell consits of a table or matrix with 6 columns and one million rows.
Column 1 shows date. Column 2 shows time. Column 3 shows max value. Column 4 shows min value. Column 5 shows closing value. Column 6 shows the average value of column 3/4.
I want to do the following:The first row and column of the cell array should have the following formula: (column 6 – (min value of row 1 to 60 of column 4))/((max value of row 1 to 60 of column 3) – (min value of row 1to 60 of column 4)). I want this formula to be applied for the first 60 rows. At row 61 i want the formula to change to: (column 6 – (min value of row 61 to 120 of column 4))/((max value of row 61 to 120 of column 3) – (min value of the row 61 to 120 of column 4)). I want this formula to jump after every 60 rows until all rows are covered. Thanks

Best Answer

AA - you seem to have a good idea of the algorithm, so it should be straightforward to implement. Suppose myData, a matrix of dimension 1000000x6, is one element in your cell array. Then
% assume that myData is 2D
[m,n] = size(myData);
% size your output column
calcData = zeros(m,1);
% iterate in steps of 60
stepSize = 60;
for k=1:stepSize:m
% set the start and end indices for the block of sixty
startIdx = k;
endIdx = min(m,k+stepSize-1);
% determine the min and max values
minCol4 = min(myData(startIdx:endIdx,4));
maxCol3 = max(myData(startIdx:endIdx,3));
% do the calculation
calcData(startIdx:endIdx) = (myData(startIdx:endIdx,6) - minCol4)/(maxCol3-minCol4);
end
Note that we do the min(m,k+stepSize-1); to make sure that we do not set the end index to something beyond the end of the array. Once the code has completed the above loop, you can do whatever you wish with the calcData column (prepend to myData for example), and then move on to the next cell array element.