フィルターのクリア

Using intersect to compare more than two values in for loop

2 ビュー (過去 30 日間)
Joel Schelander
Joel Schelander 2021 年 4 月 15 日
回答済み: Vaibhav 2024 年 5 月 29 日
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?

回答 (1 件)

Vaibhav
Vaibhav 2024 年 5 月 29 日
Hi Joel
Your current strategy effectively identifies combinations where VID1 and VID2 contain different vehicle IDs by utilizing intersect to check for common elements.
You can use several strategies to handle unique elements and intersections. First, using sets is effective; set operations like intersect, union, and setdiff are well-suited for tasks involving unique elements, and your current use of intersect is appropriate. Second, logical indexing can sometimes be faster for numeric arrays, especially for simple inclusion checks, though it may be less straightforward for cell arrays of IDs.
To extend your function to handle three vehicles (VID1, VID2, VID3) refer the below code snippet:
% Initialize vehicle ID cell arrays with example data
VID1 = {173, [150 155], 160};
VID2 = {[173 178], 190, [165 170]};
VID3 = {[180 185], [195 200], 175};
% Example matrix A for combinations (simplified for this example)
% Let's just manually specify some combinations for simplicity
A = [1 2 3; 2 3 1; 3 1 2]; % Simplified combination matrix
% Call the function
IDCELL = c3(VID1, VID2, VID3, A);
% Display the result
disp(IDCELL);
function [IDCELL] = c3(VID1, VID2, VID3, A)
IDCELL = {}; % Initialize output cell array
for oz = 1:size(A, 1)
index = A(oz,:);
% Adjusted logic for three vehicles
if numel(intersect(VID1{index(1)}, VID2{index(2)})) == 0 && ...
numel(intersect(union(VID1{index(1)}, VID2{index(2)}), VID3{index(3)})) == 0
IDCELL{end+1} = [VID1{index(1)} 0 VID2{index(2)} 0 VID3{index(3)}];
else
continue
end
end
end
Hope it helps!

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by