# Broadcast implementation

**URL:** https://discourse.julialang.org/t/broadcast-implementation/102249
**Category:** New to Julia
**Tags:** question, broadcast
**Created:** [July 29, 2023, 6:53pm UTC](https://discourse.julialang.org/t/broadcast-implementation/102249 "2023-07-29T18:53:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [July 29, 2023, 6:53pm UTC](https://discourse.julialang.org/t/broadcast-implementation/102249/1 "2023-07-29T18:53:26Z")

</div>

Hello, good afternoon

I have two data variables as below:  
data is a matrix 150x2  
centroids is a matrix 3x2

Im trying to execute broadcast like in python, and get a vector x, y to get subtracted from all 150x2 observations in data. How to perform this calculation?

```julia
data .- centroids[1, :]

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 29, 2023, 7:12pm UTC](https://discourse.julialang.org/t/broadcast-implementation/102249/2 "2023-07-29T19:12:35Z")

</div>

> [@jcbritobr](#):
>
> `data .- centroids[1, :]`

Use `data .- centroids[1, :]'` to transpose `centroids[1,:]` to a 1 \times 2 row vector.

See also [Unintuitive Julia result: selecting a row from a matrix](https://discourse.julialang.org/t/unintuitive-julia-result-selecting-a-row-from-a-matrix/66222)

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 29, 2023, 7:30pm UTC](https://discourse.julialang.org/t/broadcast-implementation/102249/3 "2023-07-29T19:30:55Z")

</div>

When you index an axis with a number, the result removes that axis, so you went from a 3x2 matrix to a 2-length vector. Broadcasting lines up to the leftmost axis not the rightmost, so 15x2 is incompatible with 2. Besides using a transpose to get a 1x2 matrix, you can actually keep the axis in the first place, you just have to index with a 1-length range instead of a number `centroids[1:1, :]`.

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [July 29, 2023, 7:40pm UTC](https://discourse.julialang.org/t/broadcast-implementation/102249/4 "2023-07-29T19:40:33Z")

</div>

Thank you guys for the help. I’m trying to port a kmeans algorithm from python to julia, and found that difference from numpy and julia. I will try the solutions and soon will return in this post.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 29, 2023, 8:09pm UTC](https://discourse.julialang.org/t/broadcast-implementation/102249/5 "2023-07-29T20:09:23Z")

</div>

It’s a bit confusing going between, it’s rooted in how C is row-major and Julia is column-major but they both try to adhere to conventional matrix notation. If it helps, here’s some pointers:

- A single dimension vector is more like a column vector with #rows in Julia, but a row vector with #columns in Numpy.
- A matrix is conventionally #rows x #columns, both Numpy and Julia adhere to this.
- When you stack matrices to go 3D, Julia continues to add axes to the right #rows x #columns x #pages. For example, a 3x2x4 array stacks 3x2 matrices. Numpy continues to add axes to the left #pages x #rows x #columns. For example, a 4x3x2 array stacks 3x2 matrices.

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [July 29, 2023, 8:24pm UTC](https://discourse.julialang.org/t/broadcast-implementation/102249/6 "2023-07-29T20:24:23Z")

</div>

Thank you very much. It worked

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/f/ffedb7d6296cd4c5be2b94be59b1d2752c7dacc6.png)
