# Randomized SVD

**URL:** <https://discourse.julialang.org/t/randomized-svd/98488>\
**Category:** General Usage\
**Tags:** question, help-database\
**Created:** [May 8, 2023, 3:44pm UTC](https://discourse.julialang.org/t/randomized-svd/98488 "2023-05-08T15:44:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Desmond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/desmond/32/47964_2.png) [@Desmond](https://discourse.julialang.org/u/Desmond)\
**Post date:** [May 8, 2023, 3:44pm UTC](https://discourse.julialang.org/t/randomized-svd/98488/1 "2023-05-08T15:44:18Z")

</div>

I am implement the randomized SVD without power iteration and I am just getting a black and white dots as the output of the SVD.

I can’t seem to spot the problem. Here is the code

```julia
function rsvd(X,k)
    (m,n)=size(X)
    Phi=rand(n,k)
    Y=X*(Phi)
    Q,R = qr(Y)
    B=transpose(Matrix(Q))*X
    Uhat,S,Vt=svd(B)
    Uk=Uhat[:, 1:k]
    Vk=Vt'[1:k, :]
    U=Matrix(Q)*Uk
    M = U * diagm(S) * Vk
    return M
    println(size(M))
    
end 

```

Let me know if you can spot the error.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [May 8, 2023, 5:37pm UTC](https://discourse.julialang.org/t/randomized-svd/98488/2 "2023-05-08T17:37:15Z")

</div>

What do you mean by black and white dots? Can you give a screenshot?  
It works on my machine.

---

<div class="post-metadata">

**Author:** ![Desmond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/desmond/32/47964_2.png) [@Desmond](https://discourse.julialang.org/u/Desmond)\
**Post date:** [May 8, 2023, 9:06pm UTC](https://discourse.julialang.org/t/randomized-svd/98488/3 "2023-05-08T21:06:00Z")

</div>

I am using the gray version of the mandrill image from the Julia TestImage packages. This is the result. Attached is the image after I use 5 singular values.

 ![jl](https://global.discourse-cdn.com/julialang/original/3X/7/a/7a6e721455096e95ce3edd2433af75d5186f453b.jpeg)

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 8, 2023, 9:29pm UTC](https://discourse.julialang.org/t/randomized-svd/98488/4 "2023-05-08T21:29:52Z")

</div>

Related question: [Compressed svd algorithm implementation - #4 by odow](https://discourse.julialang.org/t/compressed-svd-algorithm-implementation/97837/4)

@Desmond it’s easier to help if you can provide a reproducible example that people can copy-and-paste.

```julia
import TestImages
import Images
import ImageInTerminal
import LinearAlgebra

function rsvd(X, k)
    m, n = size(X)
    Φ = rand(n, k)
    Y = X * Φ
    Q, R = LinearAlgebra.qr(Y)
    Qm = Matrix(Q)
    B = Qm' * X
    Uhat, S, Vt = LinearAlgebra.svd(B)
    Uk = Uhat[:, 1:k]
    Vk = Vt'[1:k, :]
    U = Qm * Uk
    return U * LinearAlgebra.Diagonal(S) * Vk
end

image = TestImages.testimage("mandrill")
gray_image = Images.Gray.(image);
image_matrix = Images.channelview(gray_image);
M = rsvd(image_matrix, 5);
Images.colorview(Images.Gray, M)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/de80a189c51d6e7c94e5ec18e4fe805ff4ef3e60.jpeg)

---

<div class="post-metadata">

**Author:** ![Desmond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/desmond/32/47964_2.png) [@Desmond](https://discourse.julialang.org/u/Desmond)\
**Post date:** [May 8, 2023, 10:10pm UTC](https://discourse.julialang.org/t/randomized-svd/98488/5 "2023-05-08T22:10:54Z")

</div>

Sorry about the code.

I think it’s working now.

But I don’t see the difference between your code and mine. The only difference is diagm(S) and Diagonal(S) which I think doesn’t make any difference.

Or I maybe wrong?

---

<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:** [May 8, 2023, 10:14pm UTC](https://discourse.julialang.org/t/randomized-svd/98488/6 "2023-05-08T22:14:01Z")

</div>

How are you plotting the output?

It’s really crucial to include an example that could be reproduced. Otherwise, how can we know it’s not just a plotting issue, for example.

---

<div class="post-metadata">

**Author:** ![Desmond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/desmond/32/47964_2.png) [@Desmond](https://discourse.julialang.org/u/Desmond)\
**Post date:** [May 8, 2023, 10:25pm UTC](https://discourse.julialang.org/t/randomized-svd/98488/7 "2023-05-08T22:25:37Z")

</div>

Here is the code

```julia
using LinearAlgebra
using FundamentalsNumericalComputation
using TestImages
using Images

image=TestImages.testimage("mandrill")
grayimg=Gray.(image
X=channelview(grayimg)

function rsvd(X,k)
    (m,n)=size(X)
    Phi=rand(n,k)
    Y=X*Phi
    Q,R = qr(Y)
    B=transpose(Matrix(Q))*X
    Uhat,S,Vt=svd(B)
    Uk=Uhat[:, 1:k]
    Vk=Vt'[1:k, :]
    U=Matrix(Q)*Uk
    M = U * diagm(S) * Vk
    return M 
end 

M=rsvd(X,5)
Gray.(M)

```
