How to loop through values?

3 ビュー (過去 30 日間)
Noob
Noob 2024 年 10 月 4 日
コメント済み: Noob 2024 年 10 月 21 日
Let’s say I have 11 values of beta, evenly-spaced with linspace( ). And let’s say for each value of beta, I want to compute some things for a range of alpha values using alpha = linspace( ). How can I best do this? Currently, I do something tedious: I fix beta, run the code, press hold on in the Command Window, fix another value of beta, run code, press hold on, etc. I suspect there’s a better way to do this. Should I just write a for loop for beta = 0: 11 : pi? Thanks in advance.

採用された回答

Torsten
Torsten 2024 年 10 月 4 日
移動済み: Torsten 2024 年 10 月 4 日
Beta = linspace(0,1,10);
Alpha = linspace(0,2,20);
n = numel(Beta);
m = numel(Alpha);
Result = zeros(n,m);
for i = 1:n
beta = Beta(i);
for j = 1:m
alpha = Alpha(j);
Result(i,j) = alpha*beta; % Compute result depending on actual alpha and beta
end
end
  2 件のコメント
Noob
Noob 2024 年 10 月 4 日
Hi Torsten!
Let me try this now -- thanks for your answer!
Noob
Noob 2024 年 10 月 21 日
Thanks Torsten!

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

その他の回答 (0 件)

カテゴリ

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