MATLAB Answers

Getting mean row vector from a cell array of row vectors of the same size

4 views (last 30 days)
Matthew Peoples
Matthew Peoples on 26 May 2021
Answered: Allen on 26 May 2021
Hi there,
I have a cell array of 1x60, each containing a double/row vector of 1x256 - each of these a histogram of an image of values from 0 to 255.
I need to find the mean row vector/histogram across all 60 of these images i.e. the average of cells 1-256 across all 60. I understand the logic but don't know how to code it.
Any help would be very much appreciated. Thank you.

Accepted Answer

Jonas
Jonas on 26 May 2021
Edited: Jonas on 26 May 2021
so if i understood you correctly each cell contains histogram values with 256 bins each and you want to know mean count for each bin: in this case use cell2mat which converts your 1x60 cell to a 256x60 matrix. the use mean() which operates columnwise. then you get medium count for each bin

More Answers (1)

Allen
Allen on 26 May 2021
It is not entire clear to me what you are trying to average. However, if I am understanding correctly, the following example attempts to duplicate your 1x60 cell-array with 1x256 numerical arrays, where each numerical array contains a randomly assigned value from 0 to 255 for each element. I then determine the average of all values in each 1x256 numerical array and since there are 60 elements in the cell-array, the end result is a 1x60 numerical array. If this not what you are looking for please provide more details and include your an example of your initial data and a few snippets of code that you have already attempted.
% Attempts to duplicate your datasets as elements in a cell-array (each cell-array element is a 1x256 numerical array)
C = arrayfun(@(x) randi([0,255],[1,256]),1:60,'un',0);
% Calculates the overall average for each element in the cell-array
avg = cellfun(@mean,C);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!