MATLAB Answers

0

Coding for nested loop problem

Asked by Vinay Killamsetty on 20 Aug 2019 at 9:41
Latest activity Commented on by Vinay Killamsetty on 21 Aug 2019 at 13:18
Accepted Answer by Jan
I want to run nested loop with two variables 'p' and 'q'
Outer loop is for 'p' and Inner loop is for 'q'
The problem is q should run for all values when ('q' not equal to 'p') and neglect the simulation of its contents when q=p
Is there any simple method to do this other than using "for loop"

  2 Comments

Adam
on 20 Aug 2019 at 9:44
Just put an if condition in the nested loop with a
continue
statement in it.
Thank you very much Dear Adam.
Can you suggest any method to write the code for this without using for loops

Sign in to comment.

Products

1 Answer

Jan
Answer by Jan
on 20 Aug 2019 at 11:14
Edited by Jan
on 20 Aug 2019 at 11:16
 Accepted Answer

for p = 1:10
for q = 1:10
if p ~= q
...
end
end
end
Without a for loop:
x = rand(10, 10);
index = (1:10) ~= (1:10).';
mean(x(index)) % Mean of elements, which are not on main diagonal

  1 Comment

Thank you very much

Sign in to comment.