Reputation: 91
I have a 24 x 3 matrix
“point1”
and I have also 5 other 1x3 row vectors
. What I want is to compare all of the 5 different row vectors to each row of “point1”
so as to see if any of the 5 vectors have a corresponding row in “point1”
which is equal to them, and then return the index of that row in “point1”
. I have been able to do this with the following code, but i am seeking a more simple and elegant (possibly without a loop?) solution.
point1 = [7.5 4 5
8.5 4 5
9.5 4 5
10.5 4 5
11.5 4 5
7 4 5.5
12 4 5.5
6.5 4 6
12.5 4 6
6 4 6.5
13 4 6.5
5.5 4 7
13.5 4 7
5 4 7.5
14 4 7.5
5 4 8.5
14 4 8.5
5 4 9.5
14 4 9.5
5 4 10.5
14 4 10.5
5 4 11.5
14 4 11.5
5.5 4 12];
fN = [8, 4.5, 5];
fS = [8, 3.5, 5];
fE = [8.5, 4, 5];
bN = [7, 4.5, 5];
bT = [7, 4, 5.5];
for ii = 1:size(point1, 1)
indx(ii) = isequal(point1(ii,:),fN(:)') | isequal(point1(ii,:),fS(:)') | isequal(point1(ii,:),fE(:)') | isequal(point1(ii,:),bN(:)') | isequal(point1(ii,:),bT(:)')
pIndx = find(indx)
end
This returns:
pIndx = [2 6];
Thanks guys!
Upvotes: 2
Views: 75
Reputation: 12214
You can use ismember
with the 'rows'
flag to search for intersections between your vectors and your data matrix.
Using the above example it's probably easiest to concatenate your query vectors into one matrix and use that as an input:
test = find(ismember(point1, vertcat(fN, fS, fE, bN, bT), 'rows'))
Which returns:
test =
2
6
Alternatively you can make the queries individually if the individual results are important:
test_fN = find(ismember(point1, fN, 'rows'));
test_fS = find(ismember(point1, fS, 'rows'));
test_fE = find(ismember(point1, fE, 'rows'));
test_bN = find(ismember(point1, bN, 'rows'));
test_bT = find(ismember(point1, bT, 'rows'));
Upvotes: 6
Reputation: 1206
You could try the following:
[find(all(fN == point1, 2)), ...
find(all(fS == point1, 2)), ...
find(all(fE == point1, 2)), ...
find(all(bN == point1, 2)), ...
find(all(bT == point1, 2))]
Upvotes: 0