# Why is this code wrong?

**URL:** <https://discourse.julialang.org/t/why-is-this-code-wrong/72928>\
**Category:** General Usage\
**Created:** [December 11, 2021, 8:28am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928 "2021-12-11T08:28:10Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 11, 2021, 8:28am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/1 "2021-12-11T08:28:10Z")

</div>

using NearestNeighbors  
using SparseArrays  
using LinearAlgebra  
radius=.03  
box=[0,1,0,1]  
ninit= 100  
dotmax=1000  
xx=range(0,stop=1,length=ninit)  
yy=1e-4.\*rand(1,ninit)  
X=vcat(xx[:]‘,yy[:]’)  
pdp = X’  
pdp\_end = ninit  
p = zeros(dotmax,2)  
t = pdp[1:pdp\_end,2]  
y = minimum(t)  
a = argmin(t)  
global d = 0  
while y \<= box[4] && d \< dotmax  
global d = d+1  
p[d,:] = pdp[a,:]  
end

My Julia version is 1.6.2  
Why is this code wrong?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 11, 2021, 9:18am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/2 "2021-12-11T09:18:54Z")

</div>

Please have a look at this post: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) . It explains how to ask a question, to make it easier for people to understand and answer.

What is the problem you have with this code? Anyway just trying to execute it, it fails on one of the first lines: `pdp = [LinRange(box[1],box[2],ninit),box[3].+1e-4.rand(ninit,1)]` because `1e-4.rand(ninit,1)` doesn’t make sense. Do you mean `1e-4.*rand(ninit)`?

---

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 11, 2021, 10:46am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/3 "2021-12-11T10:46:34Z")

</div>

1e-4.\*rand(ninit)`,I wrote this correctly, it was copied incorrectly here  
My problem is starting the while loop.This gives the error :  
LoadError: UndefVarError: a not defined

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 11, 2021, 11:00am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/4 "2021-12-11T11:00:47Z")

</div>

BTW, you can edit your opening post, to format the code correctly (please do this), and improve it to make it clearer.

If you can, please shorten the code to only include the parts necessary for demonstrating the problem.

---

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 11, 2021, 11:08am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/5 "2021-12-11T11:08:18Z")

</div>

My problem is the while loop.This gives the error :  
LoadError: UndefVarError: a not defined

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 11, 2021, 12:28pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/6 "2021-12-11T12:28:22Z")

</div>

Your code runs without error for me. Have you tried running this in a fresh session?

---

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 11, 2021, 12:50pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/7 "2021-12-11T12:50:32Z")

</div>

I replaced && with b & my code was executed.  
is it right?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 11, 2021, 12:57pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/8 "2021-12-11T12:57:18Z")

</div>

I have no idea what you are trying to do so can’t answer that question. All I’m saying is that the code snippet in your original post (maybe after some edits you made?) runs fine, in the sense that all rows in `p` get filled with the row in `pdp` which has the lowest value in the second column. I don’t know whether this is what you’re after though.

Overall your code seems very convoluted and unnecessarily complex, are you coming from Matlab by any chance?

---

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 11, 2021, 1:06pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/9 "2021-12-11T13:06:51Z")

</div>

Yes I want to find the least value of the second column at the beginning of the code.  
I have trained Julia for a while and I would like to learn it completely, but I used to work with MATLAB. Now I am going to convert a MATLAB code to Julia.I’m sure somewhere in the code is wrong because I do not get the output I get in MATLAB in Julia.This is the code output in Julia :  
Matrix{Float64}(undef, 0, 2)  
If the output is to be a vector that I draw and get the scatter points on the coordinate plane

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 11, 2021, 1:22pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/10 "2021-12-11T13:22:17Z")

</div>

If I run the code above I end up with:

```julia
julia> p
1000×2 Matrix{Float64}:
 0.0606061 6.6019e-7
 0.0606061 6.6019e-7
 0.0606061 6.6019e-7
 ⋮          
 0.0606061 6.6019e-7
 0.0606061 6.6019e-7

```

Your entire code could be replaced with:

```julia
ninit = 100
xx = range(0, stop=1, length = ninit)
yy = 1e-4 .* rand(ninit)
pdp = hcat(xx, yy)
a = argmin(pdp[1:ninit, 2])
p = [fill(pdp[a, 1], 1_000) fill(pdp[a, 2], 1_000)]

```

to give the same result. However I have a strong suspicion that what you’re doing is suboptimal, as I struggle to think of a situation in which allocating a 1000x2 matrix which holds the same row 1,000 times is the best thing to do.

---

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 11, 2021, 1:44pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/11 "2021-12-11T13:44:55Z")

</div>

How can I send you MATLAB code?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 11, 2021, 1:49pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/12 "2021-12-11T13:49:51Z")

</div>

You can just post it here, as long as you format it correctly (i.e. in a block of triple backticks) it will render as code.

That said, just block posting some MATLAB code might not be the most productive thing to do - better to just ask specific questions about things you’re struggling with (e.g. “how do I conditionally overwrite certain rows of a matrix”) rather than giving people a big blob of code and asking “what’s wrong”.

---

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 12, 2021, 12:10pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/13 "2021-12-12T12:10:44Z")

</div>

Thank you for your help, but the rest of the code that I run is not error, but the output of the vector is zero, I do not know why?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 12, 2021, 2:46pm UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/14 "2021-12-12T14:46:01Z")

</div>

That’s impossible to tell without an MWE really. But in principle it does sound like the sort of thing that simply running your code line-by-line and checking your output vector after every step should solve.

---

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 13, 2021, 9:04am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/15 "2021-12-13T09:04:01Z")

</div>

I use atoms to work with Julia. How can the Julia code be executed line by line in the atom? I could not find it

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 13, 2021, 10:29am UTC](https://discourse.julialang.org/t/why-is-this-code-wrong/72928/16 "2021-12-13T10:29:11Z")

</div>

Depends on keymapping, but I think default is `Ctrl+Enter` or `Shift+Enter` to evaluate a single line.

Note that for a loop, Juno will automatically execute the whole code block if you evaluate any line in the loop, so you’ll have to select (highlight) the individual lines and `Ctrl+Enter` execute them.
