Clear Filters
Clear Filters

How to pick all elements from a set that satisfy a certain condition?

20 views (last 30 days)
Let's say I have a set of numbers A=-5,-4,-3,-2,-1,0,1,2,3,4,5
and I want to pick those pairs that give a sum of 1. For example, -2 and 3 is one possible pair, but how do we display all such pairs?
Any help will be much appreciated.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 9 Jul 2024 at 18:12
Edited: Fangjun Jiang on 9 Jul 2024 at 19:37
A=[-5,-4,-3,-2,-1,0,1,2,3,4,5];
index=(A+A')==1;
[I,J]=find(index);
A([I,J])
ans = 10x2
5 -4 4 -3 3 -2 2 -1 1 0 0 1 -1 2 -2 3 -3 4 -4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!