# "for" loop, collect()

**URL:** <https://discourse.julialang.org/t/for-loop-collect/48616>\
**Category:** New to Julia\
**Created:** [October 19, 2020, 10:11am UTC](https://discourse.julialang.org/t/for-loop-collect/48616 "2020-10-19T10:11:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 19, 2020, 10:11am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/1 "2020-10-19T10:11:11Z")

</div>

Hello,

I have been using “for” loop to calculate Internal Rate of Return (irr) for a range of investment options (t below represents option t in array of data) and attempted the codes below:  
for t in 1:nb  
a(t) = irr([-Market\_values[:,t];AssetsCF[:,t]])  
b(t) = irr([-Market\_values[:,t];PDCF\_annual[:,t]])  
c(t) = irr([-Market\_values[:,t];FSCF\_annual[:,t]])

```
IRR_MVCF = collect(a(t))
IRR_PDCF = collect(b(t))
IRR_FSCF = collect(c(t))

```

CSV.write(“irr\_all.csv”,DataFrame(irr\_1=a(t),irr\_2=b(t),irr\_3=c(t)),append=true)  
end

IRR\_adj = IRR\_MVCF - IRR\_PDCF + IRR\_FSCF

CSV.write(“irr\_adj.csv”,DataFrame(IRR\_adj))

As far as I understand collect() is used to transform iterators into an array. So I defined e.g. " IRR\_MVCF = collect(a(t))" in order to calculate IRR\_adj. But the error message states below showing IRR\_MVCF isn’t defined. Could someone point me out how I can refine the codes please?

_LoadError: UndefVarError: IRR\_MVCF not defined_

_in expression starting at C:\Users\XiaopingWang\Desktop\Learning\_Julia\AdjAssetsMV.jl:30_

_top-level scope at AdjAssetsMV.jl:30_

_include\_string(::Function, ::Module, ::String, ::String) at loading.jl:1088_

Many thanks  
Ping.

---

<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:** [October 19, 2020, 10:36am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/2 "2020-10-19T10:36:57Z")

</div>

Hard to say what’s going on without a reproducible example, but sounds a bit like a scope issue - if you create variables in a loop, they won’t generally be accessible outside the loop.

Further, your `a(t)` notation looks a bit odd - from what you’re doing it looks like you’re trying to index into an array, but round brackets `()` are for function calls in Julia, not for indexing. Do you come from MATLAB by any chance? If you want to store results in some array `a`, the syntax is `a[t]` (square brackets).

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [October 19, 2020, 10:54am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/3 "2020-10-19T10:54:14Z")

</div>

please kindly start your code block with

````julia
```julia

````

and close it with  
```  
right now it is very hard to read

---

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 19, 2020, 11:38am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/4 "2020-10-19T11:38:14Z")

</div>

Thanks, I think you are right it seems a scope issue. I have two “for” loop statements which I posted the first one as above. When I moved the “end”, the error message " _IRR\_MVCF not defined_" disappeared. Unfortunately, the similar error message appeared.

_ **I think the issue here is how to define array which is a result of collective loop iterators.** _ Could you please point out to me.

In addition, you are right in terms of the notations of a, the “t” represents an index of array. When changed a(t) - a[t], the codes do not work.

Happy to share the Julia codes and error message again if needed.

Many thanks,  
Ping.

---

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 19, 2020, 11:46am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/5 "2020-10-19T11:46:02Z")

</div>

Sorry for not being clear.  
…Julia

for t in 1:nb  
a(t) = irr([-Market\_values[:,t];AssetsCF[:,t]])  
b(t) = irr([-Market\_values[:,t];PDCF\_annual[:,t]])  
c(t) = irr([-Market\_values[:,t];FSCF\_annual[:,t]])

```
IRR_MVCF = collect(a(t))
IRR_PDCF = collect(b(t))
IRR_FSCF = collect(c(t))

IRR_adj = IRR_MVCF - IRR_PDCF + IRR_FSCF

CSV.write("irr_all.csv",DataFrame(irr_1=a(t),irr_2=b(t),irr_3=c(t)),append=true)
CSV.write("irr_adj.csv",DataFrame(IRR_adj=IRR_adj),append=true)

for n in 1:nb

    d(n) = npv(dot(IRR_adj'[n,:],ones(1)),vcat(0,AssetsCF[:,n])) 

    Adj_AssetMV = collect(d(n))

    CSV.write("Adj_MarketValues_new.csv",DataFrame(Adj_MarketValue_new=d(n)),append=true)
end

```

end

println(“Results were saved in separate CSV files”)

* * *

Error message  
…Julia  
ERROR: LoadError: adjoint not defined for Array{Float64,0}. Consider using `permutedims` for higher-dimensional arrays.  
Stacktrace:  
[1] error(::String) at .\error.jl:33  
[2] adjoint(::Array{Float64,0}) at D:\buildbot\worker\package\_win64\build\usr\share\julia\stdlib\v1.5\LinearAlgebra\src\transpose.jl:3  
[3] (::var"#d#128"{Array{Float64,0}})(::Int64) at C:\Users\XiaopingWang\Desktop\Learning\_Julia\AdjAssetsMV.jl:36  
[4] top-level scope at C:\Users\XiaopingWang\Desktop\Learning\_Julia\AdjAssetsMV.jl:38  
[5] include\_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1088  
in expression starting at C:\Users\XiaopingWang\Desktop\Learning\_Julia\AdjAssetsMV.jl:20  
…

Many thanks,  
Ping

---

<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:** [October 19, 2020, 11:57am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/6 "2020-10-19T11:57:25Z")

</div>

The code above still has `a(t)` rather than `a[t]`?

The error is telling you that you’re trying to get the adjoint of a zero-dimensional array: this probably happens on this line:

```julia
    d(n) = npv(dot(IRR_adj'[n,:],ones(1)),vcat(0,AssetsCF[:,n])) 

```

where I assume `IRR_adj` is zero-dimensional. Again it’s hard to say why this is the case as we can’t actually run your code ourselves, but I presume it’s because of your indexing error (using round brackets). Consider:

```julia
julia> a(t) = 5.0
a (generic function with 1 method)

julia> collect(a(1))
0-dimensional Array{Float64,0}:
5.0

```

`a(1)` is just a call to the single argument function `a` you defined when you did `a(t) = irr(...)`, and returns a zero dimensional array with whatever the result of `irr(...)` is.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 19, 2020, 1:37pm UTC](https://discourse.julialang.org/t/for-loop-collect/48616/7 "2020-10-19T13:37:04Z")

</div>

triple backtick ```  
not period `.`

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [October 19, 2020, 6:10pm UTC](https://discourse.julialang.org/t/for-loop-collect/48616/8 "2020-10-19T18:10:10Z")

</div>

If you find our tips a bit hard to understand, please read [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 20, 2020, 10:39am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/9 "2020-10-20T10:39:40Z")

</div>

Thank you Nilshg. I appreciated that the information I provided in my question wasn’t that clear as a couple of others suggested. But you have been so helpful to resolve my issue. Very much appreciated!

Have a lovely day!  
Cheers,  
Ping.

---

<div class="post-metadata">

**Author:** ![Ping](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Ping](https://discourse.julialang.org/u/Ping)\
**Post date:** [October 21, 2020, 7:46am UTC](https://discourse.julialang.org/t/for-loop-collect/48616/10 "2020-10-21T07:46:48Z")

</div>

Thank you for the tips, Josuagrw.

Regards,  
Ping.
