I started learning how to program in Julia, and I'm making a pretty simple code, but it's not working as I wish, and I'm lost because I can't find where's the error.
Basically, I have a vector like this one: (1,0,0,1,1) and I made two functions that will change the entries of the vector.
The first function needs to change every entry of the vector for 1.
The second function needs to change every entry as follows: if the entry is 1, then change it for 0, and vice versa.
I have the next code:
function vectorMethodOne(vector1)
for i = 1:length(vector1)
if vector1[i] == 0
vector1[i] = 1
end
end
return vector1
end
function vectorMethodTwo(vector1)
for i = 1:length(vector1)
if vector1[i] == 0
vector1[i] = 1
elseif vector1[i] == 1
vector1[i] = 0
end
end
return vector1
end
The problem happens when I run the code like this:
vectorEx = rand(0:1, 5)
println("Original Vector:")
println(string(vectorEx))
println("Vector using method 1:")
vectorM1 = vectorMethodOne(vectorEx)
println(string(vectorM1))
println("Vector using method 2:")
vectorM2 = vectorMethodTwo(vectorEx)
println(string(vectorM2))
The output looks like this:
> Original Vector:
> [1,0,0,1,1]
> Vector using method 1:
> [1,1,1,1,1]
> Vector using method 2:
> [0,0,0,0,0]
But I want that the output looks like this:
> Original Vector:
> [1,0,0,1,1]
> Vector using method 1:
> [1,1,1,1,1]
> Vector using method 2:
> [0,1,1,0,0]
If I only run the vectorMethodTwo, it works like I want, like this:
vectorEx = rand(0:1, 5)
println("Original Vector:")
println(string(vectorEx))
println("Vector using method 2:")
vectorM2 = vectorMethodTwo(vectorEx)
println(string(vectorM2))
And the output looks like this:
> Original Vector:
> [1,0,0,1,1]
> Vector using method 2:
> [0,1,1,0,0]
But I want that every function run over the original vector (1,0,0,1,1) but the vectorMethodTwo is running over the modified vector (1,1,1,1,1) and I can't understand where's the error in my code.