# Coding for nested loop problem

3 Ansichten (letzte 30 Tage)
Vinay Killamsetty am 20 Aug. 2019
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 KommentareKeine anzeigenKeine ausblenden
Just put an if condition in the nested loop with a
continue
statement in it.
Vinay Killamsetty am 20 Aug. 2019
Thank you very much Dear Adam.
Can you suggest any method to write the code for this without using for loops

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jan am 20 Aug. 2019
Bearbeitet: Jan am 20 Aug. 2019
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Vinay Killamsetty am 21 Aug. 2019
Thank you very much

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by