Hi,
I have a problem with the mean function. I want to have the mean of a matrix A (129x1)
but the function : c= mean (A) doesn't work.
How could I solve this Problem ?

2 件のコメント

David Young
David Young 2015 年 1 月 31 日
Do you get an error message, or is the value of c not what you expect?
Mr LE
Mr LE 2015 年 1 月 31 日
The error message is:
Undefined function 'sum' for input arguments of type 'cell'.
Error in mean (line 82) y = sum(x,dim,flag)/size(x,dim);
Error in Devoir1vo (line 16) c= mean (A)

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

 採用された回答

Star Strider
Star Strider 2015 年 1 月 31 日
編集済み: Star Strider 2015 年 1 月 31 日

1 投票

In the Command Window, type:
which mean -all
I suspect it will result with the first line being:
mean is a variable.
If it does, you will have to rename whatever your ‘mean’ variable is. (This is the most common problem when built-in functions abruptly fail to work correctly.)
EDIT —
You didn’t mention that ‘A’ was a cell. You can convert it easily to a double array by using the ‘{:}’ notation:
A = {rand(10,1)};
c = mean(A{:});

7 件のコメント

Mr LE
Mr LE 2015 年 1 月 31 日
I don't think the problem is with the mean function. Is that possible it's because of 'cell'type data ?
Star Strider
Star Strider 2015 年 1 月 31 日
Yes. When I tried it with a cell, it gave the same error.
Another, albeit more indirect, way to take the mean of a cell variable:
c = cellfun(@mean, A);
Both this and my previous version work.
Mr LE
Mr LE 2015 年 1 月 31 日
I would like directly found the mean of 129x1 cell ...
Image Analyst
Image Analyst 2015 年 1 月 31 日
Why did you ever stick your data into a cell array in the first place? If you didn't then where did it come from? xlsread() or something?? Can you just use a regular numerical array?
Star Strider
Star Strider 2015 年 1 月 31 日
@Mr LE —
The mean function will not work on cell arrays. (I thought we already established that.) You have to convert them to double arrays first. (The reason is that cell arrays can contain non-numeric data.)
The two methods I mentioned are two ways I know of to do it. Still another option is:
c = mean(cell2mat(A));
You can create your own function to take the mean of a cell. I call it ‘cellmean’ here:
cellmean = @(c) cellfun(@mean, c);
c = cellmean(A);
So long as your cell array contains only numeric data, it should work without problems.
Mr LE
Mr LE 2015 年 1 月 31 日
Thanks a lot!
Star Strider
Star Strider 2015 年 1 月 31 日
My pleasure!

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

その他の回答 (0 件)

カテゴリ

タグ

タグが未入力です。

質問済み:

2015 年 1 月 31 日

コメント済み:

2015 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by