Averaging multiple cells element by element

I have 7 cells (named 'npsoutOrg1'...'npsoutOrg7'), each with a dimension of 5*1 cells. I want to take the average of each position across all 7 cells element by element and have the input stored in a 5*1 cell named 'npsoutOrgaverage'. How do I do that?
I tried first adding element by element with cell fun, but it would only allow 2 cells to be added at one time.
npsoutOrgAVG{1}=cellfun(@plus,npsoutOrg1{1},npsoutOrg2{1},'Un',0);
instead of
npsoutOrgAVG{1}=cellfun(@plus,npsoutOrg1{1},npsoutOrg2{1},...npsoutOrg7{1},'Un',0);
Plus, if this works, I still have to do it individuall for npsOutOrgAvg{2}... to npsOutOrgAvg{5}. Is there a faster way to average elements in the same position of the cells?
Not sure if this is of importance, but here's what each cell looks like:
%ROI 7: (207:267,23:83)
npsoutOrg7=cell(5,1);
for i = 39:141
[nps,f,nps2,fx,fy] = nps2d(im{i}(207:267,23:83),0.7813);
npsoutOrg7{1}{end+1} = nps;
npsoutOrg7{2}{end+1} = f;
npsoutOrg7{3}{end+1} = nps2;
npsoutOrg7{4}{end+1} = fx;
npsoutOrg7{5}{end+1} = fy;
end
Thanks so much!

5 件のコメント

Adam
Adam 2015 年 10 月 6 日
Is there a reason your data is in cell arrays rather than just regular arrays that make operations like this far easier and quicker?
Brian
Brian 2015 年 10 月 6 日
It's the only way I knew to save and sort the outputs for nps2d. It has 5 outputs nps, f, nps2, fx, fy. I am also running it through i = 39:141 so I thought a cell format made the most sense. Is there a better way to organize this?
Stephen23
Stephen23 2015 年 10 月 6 日
編集済み: Stephen23 2015 年 10 月 6 日
Storing your data in one 3D numeric array would make this much easier to code. One of the tricks to writing neat and efficient MATLAB code is to learn how to use the dimensions of numeric arrays instead of nesting values as you might in low-level programming languages.
If you stored your data in one numeric array then this reduces to a simple mean call on that array. By choosing to use cell arrays when they are not required you just make your own life more complicated.
Brian
Brian 2015 年 10 月 6 日
I think I am getting the basic idea of what you mean by 3D array - so instead of having npsoutOrg1 to npsoutOrg7, there should just be one variable npsoutOrg? How would I actually carry it out? The tricky part is that each computation is based on a different set of coordinates...
Stephen23
Stephen23 2015 年 10 月 6 日
How to carry it out: use an internet search engine to research these topics:
  • Matrix indexing
  • Code vectorization
  • Array preallocation
  • Array indexing or multidimensional indexing

サインインしてコメントする。

回答 (1 件)

Joseph Cheng
Joseph Cheng 2015 年 10 月 6 日
編集済み: Joseph Cheng 2015 年 10 月 6 日

0 投票

% my code to create dummy data
x = randi(10,5,7);
npsoutOrgN = mat2cell(x,5,ones(1,7));
%put all into an array
%npsoutOrgN = [npsoutOrg1 npsoutOrg2 .... npsoutOrg3];
npsSum = zeros(5,1);
for ind = 1:7
npsSum = npsSum +npsoutOrgN{ind};
end
npsAve= npsSum/7
or... bypass the for loop and convert npsoutOrgN into an array and use mean()

2 件のコメント

Brian
Brian 2015 年 10 月 6 日
Thanks for the comment, but I am still getting "Undefined function 'plus' for input arguments of type 'cell'." I think this is because my data is a cell within a cell?
Joseph Cheng
Joseph Cheng 2015 年 10 月 7 日
does your data look like ex in the dummy code below?
for ind = 1:5
for jind = 1:5
ex{ind,jind} = {randi(10,1,2)};
end
end

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

2015 年 10 月 6 日

コメント済み:

2015 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by