Vector into the function showing scalar variables used

Hello! I have a "function a=fun(b,c)". The input variables b and c called into the function are both scalar. How can I create a vector into the function containing all values (b,c) called, together or separated, during the loop time in order to make accounts between new values and the previous one?

댓글 수: 2

Well, you haven’t shown us how your function looks like and the expected output.
In my specific case, I have like input, an so outside the function, scalar velues. There are no ways into the script which calls the function to vectorize the input values. To make me better understand, I inserted the structure of the function:
----------------------------------------------------------------------------------------------------------------------
function ang_rot=rotazione(t,omega)
In this case t and omega are both scalar!
Something that generates me a vector [t0 t1 t2 ... tk] (need your help!)
Something that generates me [omega0 omega1 omega2 ... omegak] (need your help!)
where k shows the current value in order to make...
diff_t=t(k)-t(k-1);
diff_omega=omega(k)-omega(k-1)
ang_rot=diff_t*diff_omega
end
----------------------------------------------------------------------------------------------------------------------
All that I need is to account, inside the function, at given time step one value with that of the previous time step considering that they are scalar as input.
Thank you for your time!

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

 채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 28일
bvals = [-2 3 4 21];
cvals = [1/2 1 2 4 8 16];
numb = length(bvals);
numc = length(cvals);
f = zeros(numb, numc);
for cidx = 1 : numc
c = cvals(cidx);
for bidx = 1 : numb
b = bvals(bidx);
f(J,K) = fun(b, c);
end
end
When you have control over fun it is often (but not always) possible to adjust it a bit to be able to calculate multiple values when b or c are vectors or arrays.

댓글 수: 3

In my specific case, I have like input, an so outside the function, scalar velues. There are no ways into the script which calls the function to vectorize the input values. To make me better understand, I inserted the structure of the function:
----------------------------------------------------------------------------------------------------------------------
function ang_rot=rotazione(t,omega)
In this case t and omega are both scalar!
Something that generates me a vector [t0 t1 t2 ... tk] (need your help!)
Something that generates me [omega0 omega1 omega2 ... omegak] (need your help!)
where k shows the current value in order to make...
diff_t=t(k)-t(k-1);
diff_omega=omega(k)-omega(k-1)
ang_rot=diff_t*diff_omega
end
----------------------------------------------------------------------------------------------------------------------
All that I need is to account, inside the function, at given time step one value with that of the previous time step considering that they are scalar as input.
Thank you for your time!
function ang_rot = rotazion(t,omega)
persistent old_t old_omega
ang_rot = 0;
if ~isempty(old_t)
ang_rot = (t-old_t) * (omega-old_omega);
end
old_t = t;
old_omega = omega;
end
Thank you very much! This function works exactly as I needed

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by