Error with cellfun using
17 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Jan
on 14 Nov 2015
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})
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!