Clear Filters
Clear Filters

Getting the mean value of a row in an array?

1 view (last 30 days)
Chris  Lambrecht
Chris Lambrecht on 17 Sep 2015
Edited: Jon on 18 Sep 2015
I have the code
filename='Laramie2005_2015.dat'; %Wind data file
yearno = 2006:2009;
for mo = 1:5
for yearidx = 1 : length(yearno)
yr = yearno(yearidx);
[dttm{mo,yearidx}, timemin{mo,yearidx}, wnddatenum{mo,yearidx}, wndspeed{mo,yearidx}, wnddir{mo,yearidx}, pres{mo,yearidx}, temp{mo,yearidx}] = RdNCDCData(filename, mo, yr, iminsamp);
which gives my desired variables as an array that is [mo,yearidx] in size with each one of the values varying in size as a 1:n row maxtix. I am looking to get the average for each row but am only getting the average of each column for each row using ave(x)=arrayfun(@(x) mean(wndspeed{x}),x) which in turn produces a matrix. Is there a way to get a single average for each month using something like that?
  1 Comment
Kirby Fears
Kirby Fears on 17 Sep 2015
I'm having trouble understanding your question. Do want to take the average (collapsing dimension 2) of an N x M cell where each cell contains a 1 x L numeric array (where L varies for each cell)? Is the desired output a N x 1 vector of averages?

Sign in to comment.

Answers (2)

Jon on 17 Sep 2015
Edited: Jon on 18 Sep 2015
If you have an array called myarray that is mo x yearidx (as you state in your question) and you want the average of all mo == j, then you could just write
meanmo(j) = mean(myarray(myarray(:,1)==j),:);
where j is the index of the month you want to average over. Just replace "myarray" with whichever variable you want to average over.
Edit: Kirby pointed out that you're using cell storage instead of matrix, so to use my method you'd need to convert your cell to a matrix first:
myarray = cell2mat(temp);
  1 Comment
Kirby Fears
Kirby Fears on 17 Sep 2015
In Chris' example, he appears to have a "mo x yearidx" cell, not an array. He's using curly brackets inside of his for loop.

Sign in to comment.

Kirby Fears
Kirby Fears on 17 Sep 2015
Edited: Kirby Fears on 17 Sep 2015
I think you're looking for one of two things, but I'm not sure which. Try these out and see if it's the output you're looking for. If not, please try to clarify your question.
** Creating example data
1. First interpretation of question: one mean per cell.
2. Second interpretation of question: one mean per "mo".
Hope this helps.


Find more on Dates and Time 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!