Error with cellfun using

17 次查看(过去 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})

更多回答(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