I want to create a for loop which creates different pairs each time.

for i=1:p
for j=1:p
i~=j
disp('i is equal to');
disp(i);
disp('j is equal to');
disp(j);
end
end
I have made this however some pairs are the same e.g i= 1 j = 1 and so on

댓글 수: 3

i ~= j
will simply output to the command line whether or not i is equal to j. The remaining code will be unaffected by whatever the result of that is.
See Akshata's response below for how to structure the if statement.
As a general rule of good practice, it is better to avoid using i and j and loop indices, MATLAB reserves these for complex numbers (i.e., sqrt(-1)). Use ii and jj instead

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

 채택된 답변

Chandrasekhar
Chandrasekhar 2015년 3월 5일
편집: Chandrasekhar 2015년 3월 5일
for i=1:p
for j=1:p
if (i~=j)
disp('i is equal to');
disp(i);
disp('j is equal to');
disp(j);
end
end
end

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Accelerators & Beams에 대해 자세히 알아보기

태그

질문:

ME
2015년 3월 5일

댓글:

ME
2015년 3월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by