Getting the mean value of a row in an array?

조회 수: 4 (최근 30일)
Chris  Lambrecht
Chris Lambrecht 2015년 9월 17일
편집: Jon 2015년 9월 18일
I have the code
filename='Laramie2005_2015.dat'; %Wind data file
iminsamp=53;
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);
end
end
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
Kirby Fears
Kirby Fears 2015년 9월 17일
Chris,
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?

댓글을 달려면 로그인하십시오.

답변 (2개)

Jon
Jon 2015년 9월 17일
편집: Jon 2015년 9월 18일
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
Kirby Fears
Kirby Fears 2015년 9월 17일
Jon,
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.

댓글을 달려면 로그인하십시오.


Kirby Fears
Kirby Fears 2015년 9월 17일
편집: Kirby Fears 2015년 9월 17일
Chris,
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
sample{1,1}=ones(1,5);
sample{1,2}=2*ones(1,6);
sample{2,1}=3*ones(1,4);
sample{2,2}=4*ones(1,5);
1. First interpretation of question: one mean per cell.
means=cellfun(@(c)mean(c),sample);
2. Second interpretation of question: one mean per "mo".
sums=cellfun(@(c)sum(c),sample);
counts=cellfun(@(c)numel(c),sample);
means=sum(sums,2)./sum(counts,2);
Hope this helps.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by