Error with cellfun using

18 ビュー (過去 30 日間)
Ozge Moral
Ozge Moral 2015 年 11 月 14 日
コメント済み: Ozge Moral 2015 年 11 月 15 日
I have a large code and I'm stuck on using cell arrays. I have some x values in cell array and i want to assign them with a function. There is a loop, function is working correct in first loop with first x cell array. But there is some error after second iteration, function didn't return true values.
x =
[-4.6250] [-3.5750]
[-3.8750] [-2.2250]
[-3.1250] [-0.8750]
[-2.3750] [ 0.4750]
[-1.6250] [ 1.8250]
[-0.8750] [ 3.1750]
[-0.1250] [ 4.5250]
[ 0.6250] [ 5.8750]
[ 1.3750] [ 7.2250]
[ 2.1250] [ 8.5750]
[ 2.8750] []
[ 3.6250] []
[ 4.3750] []
[ 5.1250] []
[ 5.8750] []
[ 6.6250] []
[ 7.3750] []
[ 8.1250] []
[ 8.8750] []
[ 9.6250] []
My function is
f = cellfun(@(x) x.^2-3.*x+5,x);
I didn't initialize it as array or anything. Error is
Error using cellfun
Non-scalar in Uniform output, at index 31, output 1.
Set 'UniformOutput' to false.
Error in neg (line 1)
f = cellfun(@(x) x.^2-3.*x+5,x);
After I did 'UniformOutput' to false as it said, but then the function didn't return minus values. I want function to work for all x values. I have missing information about cell array. Can you give me any suggestion?

採用された回答

Jan
Jan 2015 年 11 月 14 日
I do not see the problem.
C = {[-4.6250] [-3.5750]; ...
[-3.8750] [-2.2250]; ...
[-3.1250] [-0.8750]; ...
[-2.3750] [ 0.4750]; ...
[-1.6250] [ 1.8250]; ...
[-0.8750] [ 3.1750]; ...
[-0.1250] [ 4.5250]; ...
[ 0.6250] [ 5.8750]; ...
[ 1.3750] [ 7.2250]; ...
[ 2.1250] [ 8.5750]; ...
[ 2.8750] []; ...
[ 3.6250] []; ...
[ 4.3750] []; ...
[ 5.1250] []; ...
[ 5.8750] []; ...
[ 6.6250] []; ...
[ 7.3750] []; ...
[ 8.1250] []; ...
[ 8.8750] []; ...
[ 9.6250] []};
F = cellfun(@(x) x.^2-3.*x+5, C, 'Uni', false);
This works as expected and the result is correct, even for the negative values:
x = -3.5750;
disp(x.^2-3.*x+5)
disp(F{1, 2})
  1 件のコメント
Ozge Moral
Ozge Moral 2015 年 11 月 15 日
I got what was my mistake.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by