Converting a Matlab code into Julia

Thansk a lot Gunnar! This worked for me!

The replies here are obviously more immediately useful but as you continue in converting MATLAB code to Julia, if you’d like to document what you learn, I’ve created a wiki geared towards this that you could contribute to. See MATLAB to Julia wiki.

1 Like

Hello nooblia, very nice iniciative!! As soon as i end my code, i’ll look into it, and share everything i’m learning here in the discourse forums.

So guys, i have another question now… here’s a part of my code:

for i = 1:17
    i4[i] = conj((PLnlin[i]+im*QLnlin[i])/u[i]);
end

iaux1 = i4;

C = [100 0 0 0 20 0 14.3 0 0 0 9.1 0 7.7 0 0 0 5.9 0 5.3 0 0 0 4.3 0 4];
Ih = [];
for h = 3:2:25
    for i = 1:17
        if PLnlin[i] != 0
          i4[i] = (C[h]/100)*i4[i];
        end
    end
    Ih = [Ih;i4];
    i4 = iaux1;
end

And here we got:

PLnlin = [0.0 0.0 0.0 0.0 0.0 3.0e6 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]

So,

PLnlin[6] = 3e6

But when i get into i = 6, that if isn’t working. I’ve tried the following:

julia> PLnlin[6] != 0
true

Why it’s not working when i run my code? If you need any other info to analyze this, just let me know. Btw, QLnlin and u are also 1x17 vectors with numeric data.

.

Being able to copy paste the code and have it run helps.

iaux1 = i4;

looks odd, if you want a copy you need iaux1 = copy(i4).

Hello Kristoffer!

That copy(i4) worked! That’s pretty weird for me, that using iaux1 = i4; doesn’t work. Guess i’m really used to Matlab…

Thanks for your help!

A = [1,2,3]

means you have a variable A bound to an object [1,2,3]:

A --------> [1,2,3]

When you write B = A you get

A ----------v
            [1,2,3]
B-----------^

So if we do e.g. A[1] = 2 we will have

A ----------v
            [2,2,3]
B-----------^

and thus B[1] == 2.

If you do B = copy(A) you get

A --------> [1,2,3]
B --------> [1,2,3]

Now if you do A[1] = 2 you get

A --------> [2,2,3]
B --------> [1,2,3]

so in this case B[1] = 1.

28 Likes

These ASCII diagrams should go in a FAQ or somewhere.

13 Likes

Hello again!

Supose i have the following vector and a maximum value of 5 (to compare with the values in the vector).

v = [1 2 10 12 3 4 5]

How can i print something to show in wich positions of the vector v, has values that are higher than 5? I was thinking something like:

if any(v .> 5)
   println("detect wich values are higher than 5 and print their position")
else
  println("no values exceded the maximum limit")
end

but i’m not sure whats suposed to go in the argument of the println function.

julia> using Random # needed for Shuffle on Julia 0.7

julia> x = shuffle!(collect(1:10));

julia> x'
1×10 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 3  8  6  7  5  1  4  10  2  9

julia> [x[i] for i ∈ 1:10 if x[i] > 5]
5-element Array{Int64,1}:
  8
  6
  7
 10
  9

julia> [i for i ∈ 1:10 if x[i] > 5]
5-element Array{Int64,1}:
  2
  3
  4
  8
 10

So,

julia> y = [i for i ∈ 1:10 if x[i] > 5];

julia> if length(y) > 0
           println("These indices correspond to values greater than 5:\n", y)
       else
           println("no values exceded the maximum limit")
       end
These indices correspond to values greater than 5:
[2, 3, 4, 8, 10]

If you’d rather not allocate the vector y, you could just use a for loop and print. Note that any(v .> 5) allocates, anyway.

2 Likes

Great! Thanks a lot Elrod!

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