フィルターのクリア

How to make a loop run multiple times for different values of a variable.

3 ビュー (過去 30 日間)
Reinhardt RADING
Reinhardt RADING 2021 年 2 月 11 日
回答済み: Shubham Khatri 2021 年 2 月 21 日
I have a script named D.m with a variable p=rand(1).
The script has other variables (x, y,z) and it calculates the product between p and the other variables.
rng(6727);
p=rand(1);
w= p*x*y*z;
I have another script called loop.m for looping D.m fifty times.
How can i access the variables in D.m at loop.m.
I would love to have some variables change when running loop.m.
Thanks for the help.
  1 件のコメント
Stephen23
Stephen23 2021 年 2 月 11 日
This would be so much easier with functions rather than scripts:
Loop over some input vectors (indexing), call the function, store the output (indexing).

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

回答 (1 件)

Shubham Khatri
Shubham Khatri 2021 年 2 月 21 日
Hello,
You can use functions here to solve the problem. I am attaching the code to assit you with it.
x = 1;
y = 2;
z = 3;
p = rand(1);
for i = 1:50
% you change the variables over here
ans = D(x,y,z,p);
end
function [ret] = D(x,y,z,p)
ret = x*y*z*p;
end
Hope it helps

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by