Converting a Matlab code into Julia

What about just x[x .> 5]?

And obviously I meant:

(1:10)[x .> 5]

if you want the indices and not the values.

Both of those work, and subsetting is more concise and common across languages than comprehensions.
I think the comprehension is more readable though, and it doesn’t allocate a temporary for the x >. 5.

Not that printing is performance critical. Probably best to focus on whatever new users are likely to find easiest.

1 Like

Yes, I agree that a comprehension is more readable and that, in itself,
makes it worth choosing this approach. I guess that the subsetting is
possibly more comfortable for somebody converting code from MATLAB, for
instance, but it’s a short term solution.

And thanks for pointing out the extra allocation in subsetting. Could
make the difference in some codes.

Surely, this is a job for find / findall?

find(x->x>5, v)     # on 0.6
findall(x->x>5, v)  # on 0.7
3 Likes

I think findall() in 0.7 returns Cartesian Indices.
Is there a way to make it return Linear Indices like find() in 0.6 / MATLAB?
Is the only way to wrap it with Subscript to Linear function?

I’ve found it really hard to figure out how to convert an array of CartesianIndex to linear indices.

See Finding Position of Element in an Array - #9 by kristoffer.carlsson

That’s a awful memory wasting method.

What is?

1 Like

Just to be clear LinearIndices objects look and behave like arrays (because they are) but they do not require the storage of an Array.

julia> @btime LinearIndices((1000000, 1000000));
  1.302 ns (0 allocations: 0 bytes)

Hi Kristoffer,

I want to ask, which best tool that I can convert matlab code to Julia, now some tools not support any more in GitHub…

Best