Can be run two for loops in parallel?

24 visualizaciones (últimos 30 días)
ND
ND el 18 de Jun. de 2015
Respondida: Anurag Pratap Singh el 25 de Jun. de 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

Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Jun. de 2015
Editada: Walter Roberson el 18 de Jun. de 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

Más respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 18 de Jun. de 2015
Editada: Azzi Abdelmalek el 18 de Jun. de 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 el 25 de Jun. de 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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by