Error with cellfun using

조회 수: 16 (최근 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 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