finding mean for an array

조회 수: 5 (최근 30일)
santosh
santosh 2011년 3월 3일
Hi all, Just wanted to find mean for an array thnx

채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 3일
For numeric arrays:
mean(NumericArrayName(:))
I see, though, that you tagged this with "cell arrays". If you are wanting to find the mean for each cell separately, then:
cellfun(@(C) mean(C(:)), CellArrayName)
If you are wanting to find the mean of all of the cells taken together as if they were one long list of numbers:
t = cellfun(@(C) C(:), CellArrayName);
mean(vertcat(t{:}))
I am presuming in the above that when you say that you want to find the mean for an array, that you want the mean of all of the numbers in the array together. If you want the mean of the rows of an arithmetic array, then mean(NumericArrayName,2) and if you want the mean of the columns then mean(NumericArrayName)
But if what you have is a cell array containing a series of numeric arrays all the same size, and you want a position-by-position mean over all of the cells, then:
nd = ndims(CellArrayName{1});
mean(cat(nd, CellArrayName{:}), nd)
  댓글 수: 2
santosh
santosh 2011년 3월 3일
thnx alot for ur answer its very much helpful but one question i m doing a project which includes finding the mean of the different groups and plotting the mean of each group seperately
a=[164 165 166 167 175 176 177 189 190 195 196]';
b=diff(a); %find differences
idx=find(b>1); %find their indexes
idx=[idx;numel(a)]; %add the last value as index
cel=cell(1,numel(idx)); %make a cell to hold the groups
sv=1; %start value
for f=1:numel(idx)
cel{f}=a(sv:idx(f)); %take each part of a into a group
sv=idx(f)+1; %make the next start value
end
cellfun(@mean,cel)
this is helping me in knowing me the mean of each group and if possible can u help in plotting those points
Walter Roberson
Walter Roberson 2011년 3월 3일
Your code to find the mean of each group appears to be okay.
What do you want to plot against?
plot(cellfun(@mean, cel))
perhaps?
By the way:
a=[164 165 166 167 175 176 177 189 190 195 196]';
b=diff(a); %find differences
idx = find([b 2]>1); %find there indexes
cel = mat2cell(a, 1, [idx(1) diff(idx)]); %break up the matrix

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by