Search where the set of values ​​lies inside the matrix

I have two values ​​(k and j) which, as I know, are in the nx3 matrix (M). I know that they are on the same line, and that j is always to the right of k, so if k is in M ​​(2,1), then j will be in M ​​(2,2). I tested this before in a function, but now I want to find out which line is for given k and j. I need the line number of their location to continue. There are no repeated combinations of k and j in the matrix.

So, if I have a matrix

M =

1 4 5
1 5 7
k j 5
4 5 6
2 3 1

Then I want to know that they are in row 3. None of the columns are ordered.

What I tried :

I used the code below

[row,~] = find(M==k);

I am not sure how to look for a combination of them. I want to avoid using the find function. I hope to potentially use logical indexing.

? , .

+4
3

:

row = find(((M(:,1) == k ) & ( M(:,2) == j)) | ((M(:,1) == k ) & ( M(:,3) == j)) | ((M(:,2) == k ) & ( M(:,3) == j)) )

, zeros one . , find.

+1

bsxfun -

find(all(bsxfun(@eq,A(:,1:2),[k,j]),2) | all(bsxfun(@eq,A(:,2:3),[k,j]),2))

bsxfun, post on benchmarked results, .

№1:

A =
     1     4     5
     1     5     7
     6     7     1
     4     5     6
     2     3     1
k =
     6
j =
     7
>> find(all(bsxfun(@eq,A(:,1:2),[k,j]),2) | all(bsxfun(@eq,A(:,2:3),[k,j]),2))
ans =
     3

№ 2:

A =
     1     4     5
     1     5     7
     1     6     7
     4     5     6
     2     3     1
k =
     6
j =
     7
>> find(all(bsxfun(@eq,A(:,1:2),[k,j]),2) | all(bsxfun(@eq,A(:,2:3),[k,j]),2))
ans =
     3
+1

bsxfun. .

find(sum(((bsxfun(@eq,M,j) + bsxfun(@eq,M,k)) .* M).' ) == j+k >0)

1:

M = [
     1     4     5
     1     5     7
     6     7     1
     4     5     6
     2     3     1]
k=6;j=7; 

ans =  3

2:

M=[
     1     4     5
     1     5     7
     1     6     7
     4     5     6
     2     3     1
     ];
k=6;j=7;

ans =  3
+1

Source: https://habr.com/ru/post/1651051/


All Articles