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!

请先登录,再进行评论。

 采纳的回答

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