# 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:** 1
**Showing post:** 4

<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)

---

_[View the full topic](https://discourse.julialang.org/t/randomized-svd/98488)._
