Coding for nested loop problem

조회 수: 1(최근 30일)
Vinay Killamsetty
Vinay Killamsetty 2019년 8월 20일
댓글: Vinay Killamsetty 2019년 8월 21일
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
Vinay Killamsetty
Vinay Killamsetty 2019년 8월 20일
Thank you very much Dear Adam.
Can you suggest any method to write the code for this without using for loops

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2019년 8월 20일
편집: Jan 2019년 8월 20일
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
Vinay Killamsetty
Vinay Killamsetty 2019년 8월 21일
Thank you very much

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by