Can be run two for loops in parallel?

27 vues (au cours des 30 derniers jours)
ND
ND le 18 Juin 2015
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

Réponse acceptée

Walter Roberson
Walter Roberson le 18 Juin 2015
Modifié(e) : Walter Roberson le 18 Juin 2015
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

Plus de réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 18 Juin 2015
Modifié(e) : Azzi Abdelmalek le 18 Juin 2015
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 le 25 Juin 2020
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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by