In other words, I’d like to see if this row vector bt_edges(i,:) matches any row in the matrix bt_edges and return a logical vector like in the MATLAB code.
Note that logical indexing creates a temporary array (the vector saving the indices), potentially leading to slower than necessary code if they are created in a hot loop.
In MATLAB, an idiomatic way to remove unwanted values is to use logical indexing, like in the expression x(x>3) or in the statement x(x>3) = [] to modify x in-place. In contrast, Julia provides the higher order functions filter and filter!, allowing users to write filter(z->z>3, x) and filter!(z->z>3, x) as alternatives to the corresponding transliterations x[x.>3] and x = x[x.>3] . Using filter! reduces the use of temporary arrays.
Since you seem to be iterating over the rows of the matrix, maybe you can tell us more about what you want to do with those logical indices?
Also, you can get the unique rows of your matrix by a simple call to unique:
First, thank you to all of you for providing answers; it is much appreciated!
In terms of what I want to do, the next lines of MATLAB code are the following:
if sum(uval)==1
edgep = [edgep;bt_edges(i,:)];
end
The variable uval is created and then subjected to the test. The logical vector uval is summed and if it equals 1, then the edgep variable is created, which basically builds a matrix and appends the next row. Apparently, MATLAB allows for this type of recursive appending, but I’ve had to try different things in Julia to make it work, and I have not been successful.
My ultimate goal is to convert some Delaunay triangularization code from MATLAB to Julia, which I’ll share when done.
Concatenating rows is really inefficient, because you have to reallocate the entire array each time, and since Julia (like Matlab) arrays are column major, it’s especially problematic.
If you care about performance, I would suggest that you think in columns instead, then you can append data without having to make a new array each time. To do this, start with a vector, append data, and then in the end reshape into a matrix. Doing this could be 10-100x faster than appending rows (depending on sizes etc.)
Hm. I just did @btime a couple of times on my media computer, and for a couple of cases there, #2 was some 10% faster. But perhaps that was a coincidence. @benchmark probably gives a better picture.
Unless there’s some actual linear algebra taking place in the code, an even better option may be to work with a vector of vectors or a vector of tuples.
Translating Matlab code line-by-line to Julia may not be the best approach: even when you can make it work, it is very likely that it will be suboptimal and unnecessarily complicated.
Instead, I would recommend that you invest into
understanding your algorithm,
learning Julia, possibly on a simpler project,
exploring the many nice data structures that are available in packages
and build up your code from small, simple functions (which is the opposite of prevalent Matlab style).
For example, since the dimension should determine the number of edges,
I agree 100% with this but I’m looking for a solution in Julia, for the time being, that is quick and dirty. I’ve tried to find what I need but it turn out rolling my own code is the solution.
I think one of the appealing aspects of Julia is that one can open an IJulia session and interactively try out ideas.
Of course, there are more elegant solutions, but again, this is a minor part of my bigger project which is building a spatial econometrics package for Julia.
I’m almost done and need this particular code to just make sure I have an alpha version to make sure everything works before optimizing.
I would like to thank everyone for their suggestions and the great ideas.