# \#loops

**URL:** https://discourse.julialang.org/tag/loops/188.md?page=1

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

**Page:** 2

---

## [Save results from nested loop with multiple iterations](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412)

<div class="topic-metadata">

**Author:** [@Filippati](https://discourse.julialang.org/u/Filippati)\
**Replies:** 4\
**Last updated:** [January 27, 2023, 4:31pm UTC](https://discourse.julialang.org/t/save-results-from-nested-loop-with-multiple-iterations/93412 "2023-01-27T16:31:11Z")

</div>

Hello everyone! I am writing a nested loop with a structure that can be summarised with something like this: D = \[\] horizon = 40 for i in 1:7 A = function1(data\[:, i\]) # A is a MxN matrix B = function2(data\[:, …

---

## [\`for i = 1:M, j = 1:N\` discrepancy (loop vs comprehension/generator)](https://discourse.julialang.org/t/for-i-1-m-j-1-n-discrepancy-loop-vs-comprehension-generator/75659)

<div class="topic-metadata">

**Author:** [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Replies:** 26\
**Last updated:** [December 28, 2022, 2:24am UTC](https://discourse.julialang.org/t/for-i-1-m-j-1-n-discrepancy-loop-vs-comprehension-generator/75659 "2022-12-28T02:24:34Z")

</div>

I noticed the following about the for i = 1:M, j = 1:N syntax. When using this syntax as part of a normal for-loop, j iterates first: julia\> for i = 1:2, j = 1:3 println((i, j)) end (1, 1) (1, 2) (1, 3) (2…

---

## [Nested loop confusion](https://discourse.julialang.org/t/nested-loop-confusion/91068)

<div class="topic-metadata">

**Author:** [@sparrowhawk](https://discourse.julialang.org/u/sparrowhawk)\
**Replies:** 3\
**Last updated:** [December 1, 2022, 5:31am UTC](https://discourse.julialang.org/t/nested-loop-confusion/91068 "2022-12-01T05:31:44Z")

</div>

Hi all, I don’t work much with comprehensions of nested loops, but I came across something which is probably obvious but unexpected to me. Why is there a difference between these two approaches to the nested loop? Witho…

---

## [Iteration with companion matrix to make future estimates](https://discourse.julialang.org/t/iteration-with-companion-matrix-to-make-future-estimates/88911)

<div class="topic-metadata">

**Author:** [@Filippati](https://discourse.julialang.org/u/Filippati)\
**Replies:** 1\
**Last updated:** [October 19, 2022, 9:11am UTC](https://discourse.julialang.org/t/iteration-with-companion-matrix-to-make-future-estimates/88911 "2022-10-19T09:11:21Z")

</div>

Hello everyone, I have just started to learn Julia, and Julia is the only language with which I’ve worked for more than 3 months, so I am new to programming in general. I need to make 46 future quarterly estimates of in…

---

## [How do you make an arbitrarily nested for loop?](https://discourse.julialang.org/t/how-do-you-make-an-arbitrarily-nested-for-loop/88225)

<div class="topic-metadata">

**Author:** [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Replies:** 2\
**Last updated:** [October 4, 2022, 12:39pm UTC](https://discourse.julialang.org/t/how-do-you-make-an-arbitrarily-nested-for-loop/88225 "2022-10-04T12:39:10Z")

</div>

Say you have an array of ranges rngs = \[10:-1:0, 10:-4:2, 10:-3:1\]. How would you make a nested loop equivalent to inds = zeros(length(rngs)) for inds\[1\] in rngs\[1\], inds\[2\] in rngs\[2\], inds\[3\] in rngs\[3\] But for an a…

---

## [Improving the performance of a sum over a vector of vectors](https://discourse.julialang.org/t/improving-the-performance-of-a-sum-over-a-vector-of-vectors/84150)

<div class="topic-metadata">

**Author:** [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Replies:** 10\
**Last updated:** [July 13, 2022, 10:36pm UTC](https://discourse.julialang.org/t/improving-the-performance-of-a-sum-over-a-vector-of-vectors/84150 "2022-07-13T22:36:19Z")

</div>

I want to consider the sum over a vector of vectors. Consider the following benchmarks for a problem involving the sum over some data (getting a log-likelihood): using BenchmarkTools, Random, LoopVectorization Random.se…

---

## [For loop having too many allocation](https://discourse.julialang.org/t/for-loop-having-too-many-allocation/84030)

<div class="topic-metadata">

**Author:** [@jpt](https://discourse.julialang.org/u/jpt)\
**Replies:** 19\
**Last updated:** [July 11, 2022, 2:20pm UTC](https://discourse.julialang.org/t/for-loop-having-too-many-allocation/84030 "2022-07-11T14:20:36Z")

</div>

I am pretty much new to julia, and I am writing a code to train a neural network. I have loaded in my training dataset, but I find that the learning process takes too long time. When I timed my loss function, it takes 6 …

---

## [Looping in multiple dimensions extremely slow](https://discourse.julialang.org/t/looping-in-multiple-dimensions-extremely-slow/83793)

<div class="topic-metadata">

**Author:** [@dforero0896](https://discourse.julialang.org/u/dforero0896)\
**Replies:** 6\
**Last updated:** [July 6, 2022, 6:04pm UTC](https://discourse.julialang.org/t/looping-in-multiple-dimensions-extremely-slow/83793 "2022-07-06T18:04:32Z")

</div>

Hi all, I am quite new to julia and I have been rewriting some old code in Julia as practice. I am currently writing a code to compute the power spectrum of a 3D field. The code consists basically of these two functions: …

---

## [Is it possible to use Julia to speed up for loop against Python?](https://discourse.julialang.org/t/is-it-possible-to-use-julia-to-speed-up-for-loop-against-python/83596)

<div class="topic-metadata">

**Author:** [@UCDuan](https://discourse.julialang.org/u/UCDuan)\
**Replies:** 5\
**Last updated:** [July 4, 2022, 7:00am UTC](https://discourse.julialang.org/t/is-it-possible-to-use-julia-to-speed-up-for-loop-against-python/83596 "2022-07-04T07:00:41Z")

</div>

Hi all, I’m new to Julia and want to do gradient-based optimization with a time-related model. The model is pretty simple and here is a link to the python version: tonic/snow17.py at master · UW-Hydro/tonic · GitHub My…

---

## [CUDA arrays not working well with broadcast!(), and other in-place operations inside a loop](https://discourse.julialang.org/t/cuda-arrays-not-working-well-with-broadcast-and-other-in-place-operations-inside-a-loop/81965)

<div class="topic-metadata">

**Author:** [@ayushinav](https://discourse.julialang.org/u/ayushinav)\
**Replies:** 4\
**Last updated:** [June 1, 2022, 10:56am UTC](https://discourse.julialang.org/t/cuda-arrays-not-working-well-with-broadcast-and-other-in-place-operations-inside-a-loop/81965 "2022-06-01T10:56:23Z")

</div>

Hey guys, I was working on using GPUs for my codes, and came across this unusual anomaly. I am using Flux.jl along with CUDA.jl. Let’s define the variables first: rickfg= fill(0 .+ im\*0., nf, 1, 1, 1, 1, 1) |\>gpu; # F…

---

## [How to efficiently handle parallelism on a DistributedArray](https://discourse.julialang.org/t/how-to-efficiently-handle-parallelism-on-a-distributedarray/81098)

<div class="topic-metadata">

**Author:** [@ultrapoci](https://discourse.julialang.org/u/ultrapoci)\
**Replies:** 8\
**Last updated:** [May 24, 2022, 10:03am UTC](https://discourse.julialang.org/t/how-to-efficiently-handle-parallelism-on-a-distributedarray/81098 "2022-05-24T10:03:38Z")

</div>

I’m working with a DArray from DistributedArrays.jl. I want each process to work on its own part of the array independently, and access the rest of the array only when it needs a nearest neighbor which doesn’t reside in …

---

## [Creating random Static Vector by StaticVector.jl and Distributions.jl in a loop](https://discourse.julialang.org/t/creating-random-static-vector-by-staticvector-jl-and-distributions-jl-in-a-loop/78439)

<div class="topic-metadata">

**Author:** [@Vahid\_Hosseinzadeh](https://discourse.julialang.org/u/Vahid_Hosseinzadeh)\
**Replies:** 4\
**Last updated:** [March 27, 2022, 3:23pm UTC](https://discourse.julialang.org/t/creating-random-static-vector-by-staticvector-jl-and-distributions-jl-in-a-loop/78439 "2022-03-27T15:23:16Z")

</div>

Hi I wand to create a random 2 vector in a loop and want it to be static. Here is the code: using StaticArrays using Distributions dist2dN = MvNormal(\[0., 0.\],\[10. 0;0 10.\]) for \_ in 1:5 x = @SVector \[i for i in r…

---

## [Nesting a for loop in a while loop](https://discourse.julialang.org/t/nesting-a-for-loop-in-a-while-loop/78336)

<div class="topic-metadata">

**Author:** [@phantom](https://discourse.julialang.org/u/phantom)\
**Replies:** 6\
**Last updated:** [March 25, 2022, 12:24am UTC](https://discourse.julialang.org/t/nesting-a-for-loop-in-a-while-loop/78336 "2022-03-25T00:24:54Z")

</div>

Hi, I apologize if this is a very elementary mistake but I still struggling with programming and Julia. I would like to iterate through a list while a certain conditions hold true. I would also like to be able to stop…

---

## [Read every other line into a variable from a text file](https://discourse.julialang.org/t/read-every-other-line-into-a-variable-from-a-text-file/78052)

<div class="topic-metadata">

**Author:** [@Jdbeck66](https://discourse.julialang.org/u/Jdbeck66)\
**Replies:** 2\
**Last updated:** [March 17, 2022, 9:57pm UTC](https://discourse.julialang.org/t/read-every-other-line-into-a-variable-from-a-text-file/78052 "2022-03-17T21:57:53Z")

</div>

I’d like to read every other (or every third, fourth,…) line from a text file into a variable. function readtext(file) lines = String\[\] for line ∈ eachline(file) push!(lines, line::String) end r…

---

## [\[ANN\] FGenerators.jl: Python-like generator syntax for high-performance iteration](https://discourse.julialang.org/t/ann-fgenerators-jl-python-like-generator-syntax-for-high-performance-iteration/45402)

<div class="topic-metadata">

**Author:** [@tkf](https://discourse.julialang.org/u/tkf)\
**Replies:** 7\
**Last updated:** [February 19, 2022, 3:54pm UTC](https://discourse.julialang.org/t/ann-fgenerators-jl-python-like-generator-syntax-for-high-performance-iteration/45402 "2022-02-19T15:54:36Z")

</div>

FGenerators.jl is a package for defining Transducers.jl-compatible iteration protocol (aka extended foldl) with a simple Python-like @yield-based syntax. Quoting FGenerators.jl README, here are a few examples for creati…

---

## [MethodError in For loop](https://discourse.julialang.org/t/methoderror-in-for-loop/75981)

<div class="topic-metadata">

**Author:** [@Damiano](https://discourse.julialang.org/u/Damiano)\
**Replies:** 3\
**Last updated:** [February 8, 2022, 3:52pm UTC](https://discourse.julialang.org/t/methoderror-in-for-loop/75981 "2022-02-08T15:52:59Z")

</div>

This is my first post ever in this forum. I think that I have read all the relevant instructions on how to post. Still, if I am missing something let me know: I will gladly correct myself. I need to study a system of po…

---

## [Looping over previous row efficiency](https://discourse.julialang.org/t/looping-over-previous-row-efficiency/75749)

<div class="topic-metadata">

**Author:** [@korilium](https://discourse.julialang.org/u/korilium)\
**Replies:** 1\
**Last updated:** [February 3, 2022, 7:56pm UTC](https://discourse.julialang.org/t/looping-over-previous-row-efficiency/75749 "2022-02-03T19:56:14Z")

</div>

So I have a dataset with all stock prices and i want to compute the returns. I have done this the following way: test = copy(dollar\_portfolio) returns = copy(dollar\_portfolio\[2:end,:\]) @time for column in 2:length(doll…

---

## [Parallelizing a Nested Loop Concurrently](https://discourse.julialang.org/t/parallelizing-a-nested-loop-concurrently/75402)

<div class="topic-metadata">

**Author:** [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Replies:** 17\
**Last updated:** [February 1, 2022, 5:23pm UTC](https://discourse.julialang.org/t/parallelizing-a-nested-loop-concurrently/75402 "2022-02-01T17:23:03Z")

</div>

Hi! I am trying to code a problem in which I have two loops. An inner loop that a computes a sample average for a given agent using many many simulations of a particular function (which is not trivial, but each individua…

---

## [Seemingly unnecessary allocation within for loop](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556)

<div class="topic-metadata">

**Author:** [@mjg](https://discourse.julialang.org/u/mjg)\
**Replies:** 15\
**Last updated:** [December 10, 2021, 9:29pm UTC](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556 "2021-12-10T21:29:15Z")

</div>

I’m trying to create a simulation of an elastic string generalized to an arbitrary number of dimensions. To do so I track position and momentum (with an extra array to store the intermediate positions), and use the Euler…

---

## [2D nested loop gives big performance hit in interpolation routine while it should not](https://discourse.julialang.org/t/2d-nested-loop-gives-big-performance-hit-in-interpolation-routine-while-it-should-not/71651)

<div class="topic-metadata">

**Author:** [@Chiil](https://discourse.julialang.org/u/Chiil)\
**Replies:** 2\
**Last updated:** [November 17, 2021, 12:29pm UTC](https://discourse.julialang.org/t/2d-nested-loop-gives-big-performance-hit-in-interpolation-routine-while-it-should-not/71651 "2021-11-17T12:29:22Z")

</div>

I have written a macro that generates fast trilinear interpolation functions for 2x2x2 upsampling with threading. The code runs fine and is on average an order of magnitude faster than Interpolations.jl (which I probably…

---

## [Conditional for function](https://discourse.julialang.org/t/conditional-for-function/71296)

<div class="topic-metadata">

**Author:** [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Replies:** 3\
**Last updated:** [November 11, 2021, 1:11am UTC](https://discourse.julialang.org/t/conditional-for-function/71296 "2021-11-11T01:11:18Z")

</div>

Hi, I have a conditional inside the for loop, the conditional changes the function in each loop, but first i get the error “qxloc is not defined” so add qxloc (x) = nothing; but this does not solve the problem and the c…

---

## [How to create a vector of functions and index in a for loop](https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107)

<div class="topic-metadata">

**Author:** [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)\
**Replies:** 3\
**Last updated:** [November 7, 2021, 10:23pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107 "2021-11-07T22:23:41Z")

</div>

Hi, I want to create a vector of functions in julia, so that I can index in a for loop: my code is: qxloc1 = \[qxloc(x) = 10 qxloc(x) = 10\] qyloc2 = \[qyloc(x) = 10 qyloc(x) = 10\] for e = 1:2 …

---

## [Improving performance of a number of functions for large scale gradient based optimization](https://discourse.julialang.org/t/improving-performance-of-a-number-of-functions-for-large-scale-gradient-based-optimization/70980)

<div class="topic-metadata">

**Author:** [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Replies:** 11\
**Last updated:** [November 5, 2021, 2:59pm UTC](https://discourse.julialang.org/t/improving-performance-of-a-number-of-functions-for-large-scale-gradient-based-optimization/70980 "2021-11-05T14:59:30Z")

</div>

Hello, I am trying to conduct a large-scale Maximum Likelihood Estimation. Luckily, I have analytical gradients and Hessians and therefore I can speed up my optimization routine by using derivative based methods. Howev…

---

## [For loop optimization](https://discourse.julialang.org/t/for-loop-optimization/70700)

<div class="topic-metadata">

**Author:** [@claudio20497](https://discourse.julialang.org/u/claudio20497)\
**Replies:** 3\
**Last updated:** [October 31, 2021, 7:39pm UTC](https://discourse.julialang.org/t/for-loop-optimization/70700 "2021-10-31T19:39:39Z")

</div>

Hello, I’m not sure how to optimize the following for loop. array\_1 and array\_2 are two Vector{Vector{Int64}}s, and I’m interested in evaluating which combinations (of 2 elements) of their vector components has an elem…

---

## [Create multidimensional array spanned by unknown number of arrays](https://discourse.julialang.org/t/create-multidimensional-array-spanned-by-unknown-number-of-arrays/70608)

<div class="topic-metadata">

**Author:** [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Replies:** 2\
**Last updated:** [October 29, 2021, 1:02pm UTC](https://discourse.julialang.org/t/create-multidimensional-array-spanned-by-unknown-number-of-arrays/70608 "2021-10-29T13:02:23Z")

</div>

I have an (unknown) number of arrays, stored in an array: arrays = \[\[1,2,3\],\[1,2,3,4\],\[1,2,3,4,5\]\] # In this case three I want to create a multidimensional array, with one dimension for each array (so I don’t know …

---

## [Writing Type Stable Loops](https://discourse.julialang.org/t/writing-type-stable-loops/69705)

<div class="topic-metadata">

**Author:** [@dleather](https://discourse.julialang.org/u/dleather)\
**Replies:** 10\
**Last updated:** [October 13, 2021, 6:49pm UTC](https://discourse.julialang.org/t/writing-type-stable-loops/69705 "2021-10-13T18:49:20Z")

</div>

For /While loops create a local scope. Thus, if you’d like to write a function the outputs variables defined inside of the for /while loop, one must declare these variables as global beforehand, or use a let wrapper. Th…

---

## [Find frequent words](https://discourse.julialang.org/t/find-frequent-words/68790)

<div class="topic-metadata">

**Author:** [@IsraelSI](https://discourse.julialang.org/u/IsraelSI)\
**Replies:** 3\
**Last updated:** [September 27, 2021, 6:19am UTC](https://discourse.julialang.org/t/find-frequent-words/68790 "2021-09-27T06:19:47Z")

</div>

Hello colleagues, I have recently learned Julia based on the book “Getting Started with Julia” and in it there is an example of finding frequent words in a text: # code in chapter 5\\word\_frequency.jl: # 1- read in text …

---

## [Simple for loop question](https://discourse.julialang.org/t/simple-for-loop-question/67775)

<div class="topic-metadata">

**Author:** [@solaris](https://discourse.julialang.org/u/solaris)\
**Replies:** 3\
**Last updated:** [September 6, 2021, 10:18pm UTC](https://discourse.julialang.org/t/simple-for-loop-question/67775 "2021-09-06T22:18:40Z")

</div>

is there a way to arbitrarily step in a for loop? I don’t see it in the docs. For example: for i in 1:1000 println("msg: ", i) end but going 2 by 2 or 3 by 3 instead of one at a time. The equivalent of C’s: f…

---

## [Using closures for performance gain when handling datasets and making repeated function calls](https://discourse.julialang.org/t/using-closures-for-performance-gain-when-handling-datasets-and-making-repeated-function-calls/66809)

<div class="topic-metadata">

**Author:** [@rubaiyat](https://discourse.julialang.org/u/rubaiyat)\
**Replies:** 3\
**Last updated:** [August 24, 2021, 9:47am UTC](https://discourse.julialang.org/t/using-closures-for-performance-gain-when-handling-datasets-and-making-repeated-function-calls/66809 "2021-08-24T09:47:54Z")

</div>

Hello everyone, I’m in a situation where I’m estimating parameters of a model using a dataset. This requires optimization, and my question is about how I can leverage closures to speed up the process. I’d be curious to…

---

## [Plot using for loop](https://discourse.julialang.org/t/plot-using-for-loop/66167)

<div class="topic-metadata">

**Author:** [@Phonindra](https://discourse.julialang.org/u/Phonindra)\
**Replies:** 10\
**Last updated:** [August 12, 2021, 4:22pm UTC](https://discourse.julialang.org/t/plot-using-for-loop/66167 "2021-08-12T16:22:11Z")

</div>

I have ode like this Now if I need the result for a series of different values of the parameter “p”, I was trying to use for loop to do that, here is the code Which shows the following error! Please help me t…

[Previous page](https://discourse.julialang.org/tag/loops/188.md?match_all_tags=true&tags%5B%5D=loops)

[Next page](https://discourse.julialang.org/tag/loops/188.md?match_all_tags=true&page=2&tags%5B%5D=loops)
