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')

类别

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