필터 지우기
필터 지우기

Error declaring and summing array of functions - Cell contents assignment to a non-cell array object

조회 수: 1 (최근 30일)
I need to sum four functions with common data like this:
TE1 = [180 310 455 600; 0.18 0.25 0.34 0.41];
S = [16.6 19.2 21.7];
eps1 = @(B,bt,n,t)(B * (S(1)^n) / (1 - bt) * t^(1 - bt));
f1m = zeros(4,1);
for i = 1:4
f1m{i} = @(B,bt,n)(((eps1(B,bt,n,TE1(1,i)) - TE1(2,i)) / TE1(2,i))^2);
end
f1 = @(B,bt,n)(sum(f1m(B,bt,n)));
and i get
Cell contents assignment to a non-cell array object.
Error in optim_fi (line 28)
f1m{i} = @(B,bt,n)(((eps1(B,bt,n,TE1(1,i)) - TE1(2,i)) / TE1(2,i))^2);
Can anybody help me solve my problem?

채택된 답변

Jan
Jan 2017년 3월 26일
편집: Jan 2017년 3월 26일
f1m was created as double vector:
f1m = zeros(4,1);
Then you need round parenthesis, not curly braces for indexing as in cell arrays:
for k = 1:4
f1m(k) = @(B,bt,n)(((eps1(B,bt,n,TE1(1,i)) - TE1(2,k)) / TE1(2,k))^2);
end
I've replaced i by k, see Answers:i-and-j-as-variable-names
[EDITED] Sorry, the zeros() was the problem, not the assignment:
f1m = cell(4,1);
for i = 1:4
f1m{i} = @(B,bt,n)(((eps1(B,bt,n,TE1(1,i)) - TE1(2,i)) / TE1(2,i))^2);
end
  댓글 수: 2
Denis Perotto
Denis Perotto 2017년 3월 26일
That didn't help
TE1 = [180 310 455 600; 0.18 0.25 0.34 0.41];
S = [16.6 19.2 21.7];
eps1 = @(B,bt,n,t)(B * (S(1)^n) / (1 - bt) * t^(1 - bt));
f1m = zeros(4,1);
for k = 1:4
f1m(k) = @(B,bt,n)(((eps1(B,bt,n,TE1(1,k)) - TE1(2,k)) / TE1(2,k))^2);
end
f1 = @(B,bt,n)(sum(f1m(B,bt,n)));
Result
Conversion to double from function_handle is not possible.
Error in optim_fi_1 (line 6)
f1m(k) = @(B,bt,n)(((eps1(B,bt,n,TE1(1,k)) - TE1(2,k)) / TE1(2,k))^2);
Jan
Jan 2017년 3월 26일
@Denis: Try it the other way around: leave the assignment, change the definition of f1m. See [EDITED]

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by