필터 지우기
필터 지우기

How to change anonymous function to take array parameter and subject to ranges

조회 수: 2 (최근 30일)
Hi, I have a working anonymous function that takes a vector of size 1x3 and I want to expand this to 3x3. The function is named mu_x and looks like this
mu_x=@(t) f_lx(t,param);
function res=f_lx(x,param)
a=param(1);
b=param(2);
c=param(3);
res = zeros(size(x));
ind = x>100;
res(ind) = a+b*exp(c*100)+(x(ind)-100)*0.001;
res(~ind) =a+b*exp(c*x(~ind));
end
The 1x3 param is now [a b c] and the 3x3 parameter I want to use is subject to x such as:
x=65-59 param(:,1)
x=70-76 param(:,2)
x=77-106 param(:,3)
just to clarify the new param looks something like this
a1 b1 c1
a2 b2 c2
a3 b3 c3
How can the anonymous function be changed to consider the ranges of x and new format of param?
  댓글 수: 2
Matt J
Matt J 2018년 10월 30일
편집: Matt J 2018년 10월 30일
That depends. In what way is res supposed to change now that params is 3x3?
Orongo
Orongo 2018년 10월 31일
Good question. The result should be a vector.

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

채택된 답변

James Tursa
James Tursa 2018년 10월 31일
편집: James Tursa 2018년 10월 31일
You need to be careful with how you build your anonymous function:
mu_x = @(t) f_lx(t,param);
In the above line, t is an argument that can change with every call, but param is a fixed snapshot of whatever it happens to be at the time you create mu_x. Changing param downstream in your code will have no effect on mu_x. The param in mu_x will still be that fixed snapshot of the param that existed at the time mu_x was created. If you want to use a new param with mu_x, you will need to recreate mu_x from scratch (i.e., execute the above line again).
Bottom line: If you originally created mu_x with a 1x3 param, and then downstream in your code change param in any way (to a new 1x3 or to a 3x3 etc), then you will have to recreate mu_x from scratch in order for it to use that new param.
Simpler: Just call f_lx directly ... what do you need that mu_x for?

추가 답변 (1개)

Orongo
Orongo 2018년 10월 31일
I tried following change in the function but get the error
Operands to the || and && operators must be convertible to logical scalar values.
Error in f_lx (line 19)
ind1 = x>=65 && x<=69;
I know my solution won't win the price for code prettiness, but it hopefully shows you the logic I'm after.
function res=f_lx(x,param)
res = zeros(size(x));
a1=param(1,1); a2=param(2,1); a3=param(3,1);
b1=param(1,2); b2=param(2,2); b3=param(3,2);
c1=param(1,3); c2=param(2,3); c3=param(3,3);
ind1 = x>=65 && x<=69;
ind2 = x>=70 && x<=76;
ind3 = x>=77 && x<=100;
ind4 = x>100;
res(ind1) =a1(ind1)+b1(ind1).*exp(c1(ind1).*x(ind1));
res(ind2) =a2(ind2)+b2(ind2).*exp(c2(ind2).*x(ind2));
res(ind3) =a3(ind3)+b3(ind3).*exp(c3(ind3).*x(ind3));
res(ind4) = a3(ind4)+b3(ind4).*exp(c3(ind4)*100)+(x(ind4)-100)*0.001;
end
  댓글 수: 4
Orongo
Orongo 2018년 10월 31일
Changing to & helped but the function is not working. I'm calling f_lx by following
exp(-integral(mu_x,0,66))
and get the error
Index exceeds matrix dimensions.
Error in f_lx (line 24)
res(ind1) =a1(ind1)+b1(ind1).*exp(c1(ind1).*x(ind1));
Orongo
Orongo 2018년 10월 31일
Torsten, I might understand this differently. For example a1(ind1) will be a1 if ind1 is true, is this not right?

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

카테고리

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