MATLAB: Averaging and Storing Stacked Cell Array With ‘NaN’ Values

#average #cellarray #nan #stack

I have a stacked cell array called 'nitrogenCell' (Image 1 below). When I click on each of the 450×450 double, I get 'nitrogenCell{1,1}' (Image 2 below), 'nitrogenCell{2,1}' (Image 3 below), and 'nitrogenCell{3,1}' (Image 4 below), respectively. I want to average each position of each different matrix and store the resulting average value in a new matrix. For example, the first position in 'nitrogenCell{1,1}' is (1,1) and it's value is 1.4730e-04, the first position of 'nitrogenCell{2,1}' is (1,1) and it's value is -7.7473e-05, and the first position of 'nitrogenCell{3,1}' is (1,1) and it's value is 1.3875e-04. I want to average these values and store them in position (1,1) of a new matrix called 'nitrogenCellAveraged'. Then I want to continue to do this for every position of each matrix within the cell array. So next, I would average the values of positions (1,2) of each 'nitrogenCell' matrix and store it in position (1,2) of the same new matrix called 'nitrogenCellAveraged', and so on. Note also that these matrices contain 'NaN' values in certain positions and I want to ignore these 'NaN' values in the averaging process. I know a 'for loop' is ideal, but I am not sure how to do this. I am new to MATLAB. Thank you for your help!
(Image 1)
(Image 2)
(Image 3)
(Image 4)

Best Answer

Averages = mean( cat(3,nitrogenCell{:}) ,3,'omitnan');