Printing in for loop

In the following for loop, the cases i = 10,20 for d = 1 are not printed:

using LinearAlgebra, Random, Distributions
p = 1000
Σ = I(p)
X = zeros((p,p))
for d in 1:5
    println("\nd: $d")
    for i in 1:20
        if i % 10 == 0
            print(" $i, ")
        end
        X = rand(MvNormal(zeros(p), Σ), 1000)
    end
end

The code prints the following:

d: 1
 
d: 2
 10,  20, 
d: 3
 10,  20, 
d: 4
 10,  20, 
d: 5
 10,  20,

Update: It seems it’s a problem with JuliaPro. It works as expected if I copy-paste directly into the REPL, but if I use command+enter in the editor I get the strange result.

I am a bit intrigued by the behavior and would appreciate it if someone could explain the logic behind it.
If I comment out X = rand(MvNormal(zeros(p), Σ), 1000) then it works as expected.
Here’s my Julia and System info:

Julia Version 1.4.2
Commit 44fa15b150* (2020-05-23 18:35 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-8.0.1 (ORCJIT, goldmont)
Environment:
  JULIA_NUM_THREADS = 4
  JULIA_PKG_SERVER = pkg.juliacomputing.com

Weird. Your example works for me on Julia 1.4.2, Linux x86. Maybe someone with macOS can reproduce?

1 Like

either a Terminal related bug or OS related one

1 Like

Thanks. It seems it’s a problem with JuliaPro. It works as expected if I copy-paste directly into the REPL, but if I use command+enter in the editor I get the strange result.

I remember having similar issues with hidden whitespace or whatever characters which messed up things when I copy pasted code from Jupyter notebooks. Someone who copied my code and pasted it in their terminal noticed it because of the missing unicode representation (they saw the block with the questionmark). My terminal simply “showed” or ignored the invisible char.

2 Likes

Not answering your question, but: you are allocating X in each loop iteration. You need X .= ... instead.

2 Likes

Thanks, that’s a good point.