Can be run two for loops in parallel?

36 ビュー (過去 30 日間)
ND
ND 2015 年 6 月 18 日
回答済み: Anurag Pratap Singh 2020 年 6 月 25 日
I have two for loops need to be run in such away like:
for i= 1:2:9 (this represents variable like force)
for j= .1:.2:.9 ( this represents variables like displacement). each j value corresponds to i value such as i=1 so j=.1 ,then i=3 , j= .3 and so on.
Is that possible? Many thanks

採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 18 日
編集済み: Walter Roberson 2015 年 6 月 18 日
ivals = 1:2:9;
jvals = .1:.2:.9;
for K = 1 : length(ivals)
i = ivals(K);
j = jvals(K);
... do you work
end
or
for ij = [1:2:9; .1:.2:.9] %tricky use of for!
i = ij(1);
j = ij(2);
... do your work
end

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 6 月 18 日
編集済み: Azzi Abdelmalek 2015 年 6 月 18 日
j= .1:.2:.9
k=0;
for i= 1:2:9 (this represents variable like force)
k=k+1
a=j(k)
% your code
end
or
jj= .1:.2:.9
ii= 1:2:9
for k=1:numel(j)
a=jj(k)
b=ii(k)
% your code
end
avoid using i or j, because they are used in complex numbers

Anurag Pratap Singh
Anurag Pratap Singh 2020 年 6 月 25 日
Hi Nd
I understand that you are tying to run parallel loops in your code
I would suggest you to use a single loop and access both the force and disp array using the loop index
force=1:2:9
displacement=0.1:0.2:0.9
for i=1:length(force)
%Code
force(i)*dispacement(i)
end
Thank you

カテゴリ

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