Hi!
I have a 16x8 cell (containing 6x11 cells) with the name FinalResult. I now would like to calculate the mean value of FinalResult{1:16,4}(1,2) and have tried it with
cellfun(@mean,FinalResult{1:16,4}(1,2))
but this will give me the error message "Bad cell reference operation". Where is the mistake?
Thank you in advance for any help.

 採用された回答

Guillaume
Guillaume 2014 年 10 月 11 日
編集済み: Guillaume 2014 年 10 月 11 日

1 投票

I'm assuming that the cell array contains matrices not more cell arrays, that is the 6x11 are actually matrices. In which case, this would work:
mean(cellfun(@(c) c(1,2), FinalResult(:, 4)));
What it does is:
  1. Create a cell array that is a subset of your whole array with FinalResult(:, 4). Note that I use () instead of {}. The former returns a cell array, the latter a list of elements.
  2. Iterate over that subset and extract element (1, 2) of each matrix, with cellfun.
  3. get the mean of the elements returned by cellfun

1 件のコメント

Pinga
Pinga 2014 年 10 月 11 日
Thank you a lot - this works great!

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

その他の回答 (1 件)

SK
SK 2014 年 10 月 11 日
編集済み: SK 2014 年 10 月 11 日

2 投票

Put square brackets around the cellarray contents like:
M = [FinalResult{1:16,4}]
m = mean(M,2);
Putting the square brackets converts it into a matrix.

3 件のコメント

Pinga
Pinga 2014 年 10 月 11 日
Hi SK
Thank yo for your reply. This was not the solution I am looking for, since I want to calculate the mean of all (1,2) of every double (not column) in the cell FinalResult{1:16,4}. So, basically this is what I want to calculate:
((FinalResult{1,4}(1,2))+(FinalResult{2,4}(1,2))+(FinalResult{3,4}(1,2))+....+(FinalResult{1,16}(1,2)))/length(FinalResult)
Any help would be appreciated.
SK
SK 2014 年 10 月 11 日
OK, I didn't read the question properly.
In that case:
C = FinalResult(1:16, 4);
D = [C{:}];
E = reshape(D, [6,11,16]);
M = cell2mat(E);
Mx = mean(M,3);
Mx(i,j) should then contain the mean of the index (i,j).
Pinga
Pinga 2014 年 10 月 11 日
Thank you! Unfortunately, I got a error message for M ("Cell contents reference from a non-cell array object"). But the solution from Guillaume works - problem solved.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2014 年 10 月 11 日

コメント済み:

2014 年 10 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by