# Subtract the Column Wise Mean of a Large Matrix

**URL:** <https://discourse.julialang.org/t/subtract-the-column-wise-mean-of-a-large-matrix/61740>\
**Category:** General Usage\
**Created:** [May 24, 2021, 7:04pm UTC](https://discourse.julialang.org/t/subtract-the-column-wise-mean-of-a-large-matrix/61740 "2021-05-24T19:04:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [May 24, 2021, 7:04pm UTC](https://discourse.julialang.org/t/subtract-the-column-wise-mean-of-a-large-matrix/61740/1 "2021-05-24T19:04:07Z")

</div>

As a service to export from a nice discussion (Not by me) in Slack.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89ad90ce5e968f5b8d8f3e30e00c26a0855a2096.png)

> I need to take a large matrix and subtract the mean columnwise and inplace. Is there a fast way to do this?  
> By columnwise I mean taking the mean of the column and subtracting it.

Answer by @Oscar_Smith:

```julia
for i in axes(A,2)
     @views A[:,i] .-= mean(A[:,i])
end

```

By @mcabbott

> Or just `A .-= mean(A, dims=1)`

The loop is faster for large arrays.

Note that it is equivalent to `A[:,i] .= A[:,i] .- mean(A[:,i])`. Without `@views`, both of the slices on the right would allocate.
