Why is this code wrong?

using NearestNeighbors
using SparseArrays
using LinearAlgebra
ninit= 100
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,:]

My Julia version is 1.6.2
Why is this code wrong?

Please have a look at this post: Please read: make it easier to help you . 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)?


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

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.

1 Like

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

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

1 Like

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

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?


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

If I run the code above I end up with:

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:

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.


How can I send you MATLAB code?

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”.


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?

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.

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

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.

1 Like