Can be run two for loops in parallel?
27 views (last 30 days)
Show older comments
ND
on 18 Jun 2015
Answered: Anurag Pratap Singh
on 25 Jun 2020
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
0 Comments
Accepted Answer
Walter Roberson
on 18 Jun 2015
Edited: Walter Roberson
on 18 Jun 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
0 Comments
More Answers (2)
Azzi Abdelmalek
on 18 Jun 2015
Edited: Azzi Abdelmalek
on 18 Jun 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
0 Comments
Anurag Pratap Singh
on 25 Jun 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
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!