Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if a record exists in a Julia array

Say we have a matrix:

A = [1.0 2.0 3.0; 4.0 5.0 6.0] #2×3 Matrix{Float64}

and a record:

b = [1.0 2.0 3.0] #1×3 Matrix{Float64}

what is the most efficient way to check if record b exists in matrix A in Julia?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Doing b in A returns false.

And writing a nested for-loop when we may have a large matrix (many dimensions and many rows) to check seems inefficient.

>Solution :

If the rows in the matrix are unsorted I would just do:

findfirst(==(vec(b)), eachrow(A))

If the rows are sorted (which is recommended when you do the search many times) I would consider using searchsorted over a vector of views of array rows.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading