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 件のコメント

madhan ravi
madhan ravi 2019 年 4 月 28 日
Well, you haven’t shown us how your function looks like and the expected output.
Heorhii Heorhiichuk
Heorhii Heorhiichuk 2019 年 4 月 28 日
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 日

0 投票

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 件のコメント

Heorhii Heorhiichuk
Heorhii Heorhiichuk 2019 年 4 月 28 日
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 日
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
Heorhii Heorhiichuk
Heorhii Heorhiichuk 2019 年 4 月 29 日
Thank you very much! This function works exactly as I needed

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet 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