How can I process all the possible pairs of arrays without using 2 nested for loops?

조회 수: 2 (최근 30일)
I have a number of arrays and I want to compute a distance (using a home made function) between all possible pairs.
Right now I have 2 for loops like this
for i=1:(N-1)
for j=(i+1):N
applymyfunction (array(i), array(j))
end
end
I was wondering if there was a way to do this without using nested loops.
Thanks - F.
  댓글 수: 1
Stephen23
Stephen23 2024년 3월 18일
편집: Stephen23 2024년 3월 18일
I don't see any particular disadvantage in using loops:
  • loops are efficient,
  • the intent is clear,
  • debugging is easy,
  • your function will get called exactly how many times it needs to be called.
What is your specific reason for avoiding loops?

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

답변 (1개)

Walter Roberson
Walter Roberson 2024년 3월 17일
if all(triu(FirstMatrix) == triu(SecondMatrix), 'all')
  댓글 수: 2
FredMat
FredMat 2024년 3월 18일
편집: FredMat 2024년 3월 18일
Maybe "compare" was not the right word here.
I rewrote my question. Hopefully, it's more clear.
Walter Roberson
Walter Roberson 2024년 3월 18일
pdist2() or pdist(), specifying a function handle as the distance function.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by