# Using intersect to compare more than two values in for loop

1 view (last 30 days)
Joel Schelander on 15 Apr 2021
I have a function:
function [IDCELL]=c2(VID1,VID2,,A)
for oz=1:nchoosek(9,2)
index= A(oz,:);
if numel(intersect(VID1{index(1)},VID2{index(2)}))
continue
else
IDCELL{oz}=[VID1{index(1)} 0 VID2{index(2)}];
end
end
end
A is a 36x2 matrix containing possible that will contain all possible combinations of two elements in a sample of 9 elements.
VID1,VID22 are 3x3 cells. The cells of VID contains ID numbers of vehicles. There can be one or two numbers in one cell.
index is which numbers that are combined. so if index=[1 2] The function will take the first element in cell VID1 and the second element in cell VID2.
In this case the values will be:
VID1[index(1)}=173
VID2{index(2)}=[173 178]
My if statement makes sure that combinations where VID1 and VID2 contain the same vehicle, which is the case above, are avoided.
I now have two questions:
1. Is this strategy the way to go? Or is there a function for this or
2. If I would have 3 vehicles (VID1,2,3) how can I change my if statement so I ensure that only combinations where VID1,2,3 have different ID numbers?

R2017b

### Community Treasure Hunt

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

Start Hunting!