function of two variables

1 回表示 (過去 30 日間)
saja mk
saja mk 2020 年 9 月 18 日
コメント済み: madhan ravi 2020 年 9 月 18 日
i had a function with two variables f(x1,x2)
in loop for k times
how can i calculate the value of the function in each iteration??

回答 (1 件)

madhan ravi
madhan ravi 2020 年 9 月 18 日
C = cell(numel(x1));
for k = 1:numel(x1)
C{k} = f(x1(k), x2(k)); % of f() is vectorised you won’t need a loop
end
celldisp(C)
  2 件のコメント
saja mk
saja mk 2020 年 9 月 18 日
thank you alot for your reply , but can you please explain what you meant by
C = cell(numel(x1)); ??
madhan ravi
madhan ravi 2020 年 9 月 18 日
I was preallocating variable C , which makes the code more optimised . You could look up preallocation and explore why it’s important.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by