From: H on
Hi all,

I'm trying to find an easy solution for the following problem:

I have a matrix Nx4 in which I have stored coordinates of nodes as in

V=
[1 0 0 0;
2 1 0 0;
3 2 0 0;
..
..
..
N x y z]

My "problem" is that I want to find the number(row) of a certain coordinate example (2,5,6). I know I can do this with logical indexing by multiplying each column like this:

find((V(:,2)==2).*(V(:,3)==5).*(V(:,4)==6))

(I know the first column is not needed for this, but I needed in another part of my code).

The size of the V is approximately 320,000 x 4 and I have to do this quite many times so speed is essential. Using tic-toc it says this operation took 0.02 seconds. Is there a way to do this faster?

Thank you in advance,
H
From: Jos (10584) on
"H " <mesta2000(a)hotmail.com> wrote in message <hjji42$d5s$1(a)fred.mathworks.com>...
> Hi all,
>
> I'm trying to find an easy solution for the following problem:
>
> I have a matrix Nx4 in which I have stored coordinates of nodes as in
>
> V=
> [1 0 0 0;
> 2 1 0 0;
> 3 2 0 0;
> .
> .
> .
> N x y z]
>
> My "problem" is that I want to find the number(row) of a certain coordinate example (2,5,6). I know I can do this with logical indexing by multiplying each column like this:
>
> find((V(:,2)==2).*(V(:,3)==5).*(V(:,4)==6))
>
> (I know the first column is not needed for this, but I needed in another part of my code).
>
> The size of the V is approximately 320,000 x 4 and I have to do this quite many times so speed is essential. Using tic-toc it says this operation took 0.02 seconds. Is there a way to do this faster?
>
> Thank you in advance,
> H

No need for multiplication: use the logical operator &

% some data
N = 320000 ;
A = ceil(100*rand(N,4)) ;
A(:,1) = 10*(1:size(A,1)) ;
R = A(round(N/2),2:4) ; % desired row
% engine
tic
idx = find(A(:,2)==R(1) & A(:,3)==R(2) & A(:,4)==R(3))
toc

Also, take a look at ISMEMBER, using the 'rows' option (although this runs much slower):
idx = find(ismember(A(:,2:4),R,'rows')) % == N/2


hth
Jos
From: us on
"H " <mesta2000(a)hotmail.com> wrote in message <hjji42$d5s$1(a)fred.mathworks.com>...
> Hi all,
>
> I'm trying to find an easy solution for the following problem:
>
> I have a matrix Nx4 in which I have stored coordinates of nodes as in
>
> V=
> [1 0 0 0;
> 2 1 0 0;
> 3 2 0 0;
> .
> .
> .
> N x y z]
>
> My "problem" is that I want to find the number(row) of a certain coordinate example (2,5,6). I know I can do this with logical indexing by multiplying each column like this:
>
> find((V(:,2)==2).*(V(:,3)==5).*(V(:,4)==6))
>
> (I know the first column is not needed for this, but I needed in another part of my code).
>
> The size of the V is approximately 320,000 x 4 and I have to do this quite many times so speed is essential. Using tic-toc it says this operation took 0.02 seconds. Is there a way to do this faster?
>
> Thank you in advance,
> H

one of the many solutions

m=magic(3);
v=m(2,:);
im=ismember(m,v,'rows')
%{
% im =
0
1
0
%}

us
From: Oleg Komarov on
"H "
> Hi all,
>
> I'm trying to find an easy solution for the following problem:
>
> I have a matrix Nx4 in which I have stored coordinates of nodes as in
>
> V=
> [1 0 0 0;
> 2 1 0 0;
> 3 2 0 0;
> .
> .
> .
> N x y z]
>
> My "problem" is that I want to find the number(row) of a certain coordinate example (2,5,6). I know I can do this with logical indexing by multiplying each column like this:
>
> find((V(:,2)==2).*(V(:,3)==5).*(V(:,4)==6))
>
> (I know the first column is not needed for this, but I needed in another part of my code).
>
> The size of the V is approximately 320,000 x 4 and I have to do this quite many times so speed is essential. Using tic-toc it says this operation took 0.02 seconds. Is there a way to do this faster?
>
> Thank you in advance,
> H
You can use '&' instead of '.*' to combine logical arrays and if you don't need explicitly the find you'll save 25% of time on each indexing operation.

Else, you can transform V into a n by 1 matrix.
V = ...
[1 0 0 0
2 1 0 0
3 2 0 0];

[row, col] = size(V);

V2 = sum([V(:,1).*10^(col-1), V(:,2).*10^(col-2),V(:,3).*10^(col-3),V(:,4)],2);
IDX = V2 == 3200;

Oleg
From: H on
The &-operator seemed to be quite fast compared to my approach.

Thank you very much for the help!