Can be run two for loops in parallel?

24 views (last 30 days)
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

Accepted Answer

Walter Roberson
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

More Answers (2)

Azzi Abdelmalek
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

Anurag Pratap Singh
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!