# One line updating of rows of a matrix

**URL:** https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845
**Category:** New to Julia
**Created:** [June 30, 2021, 4:59pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845 "2021-06-30T16:59:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)
#### Post date: [June 30, 2021, 4:59pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845/1 "2021-06-30T16:59:40Z")

</div>

I have a simple recursive procedure to populate a matrix which can be made easier by populating row by row. i.e. calculating the row and copying it to the matrix. I can’t find a way of making it work and returning the matrix of stacked rows. I can do the calculation elementwise but that’s rather unnatural given the theory.

```julia
P = [0.8 0.1 0.1; 0.1 0.7 0.2; 0.05 0.05 0.9]
init_vec = [0.1 0.8 0.1]

function Find_L(init_vec, P)
    L = zeros(3,3)
    L[1,:] = init_vec
    [L[t,:] = transpose(P)*L[t-1,:] for t in 2:size(P,2)] 
    return L
end 

```

Is there a nice way of doing something like this that returns a matrix?

---

<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: [June 30, 2021, 5:10pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845/2 "2021-06-30T17:10:29Z")

</div>

> [@Mark\_Williams](#):
>
> ```julia
> P = [0.8 0.1 0.1; 0.1 0.7 0.2; 0.05 0.05 0.9]
> init_vec = [0.1 0.8 0.1]
> 
> function Find_L(init_vec, P)
> L = zeros(3,3)
> L[1,:] = init_vec
> [L[t,:] = transpose(P)*L[t-1,:] for t in 2:size(P,2)] 
> end 
> 
> ```

You forgot to return `L` from the function.

---

<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: [June 30, 2021, 5:14pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845/3 "2021-06-30T17:14:23Z")

</div>

As @DNF says, though I think that’s an abuse of array comprehension. It creates a useless array that is immediately thrown away, and it obfuscates the meaning of the code. What’s wrong with a plain old `for` loop?

```julia
function Find_L(init_vec, P)
    L = zeros(3,3)
    L[1,:] = init_vec
    for i = 2:size(P,2)
        L[i,:] = transpose(P)*L[i-1,:]
    end
    return L
end

```

---

<div class="post-metadata">

### Author: ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)
#### Post date: [June 30, 2021, 5:19pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845/4 "2021-06-30T17:19:24Z")

</div>

Mostly a supervisor who likes my code to read in a certain way. I agree there shouldn’t be anything wrong with the above.

---

<div class="post-metadata">

### Author: ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)
#### Post date: [June 30, 2021, 5:20pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845/5 "2021-06-30T17:20:02Z")

</div>

Yep that’s a typo. Thanks

---

<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: [June 30, 2021, 5:25pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845/6 "2021-06-30T17:25:57Z")

</div>

Also, keep in mind that Julia arrays are column major, so it is faster and more natural to work with columns than rows.

---

<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: [June 30, 2021, 5:29pm UTC](https://discourse.julialang.org/t/one-line-updating-of-rows-of-a-matrix/63845/7 "2021-06-30T17:29:05Z")

</div>

The loop @sijo wrote is _much_ clearer and easier to read. Change the orientation to avoid `transpose`, use a loop, and stand up to your supervisor 😉 They will respect you more if you do 😃
