Broadcast implementation

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?

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


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, :].

1 Like

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.

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.
1 Like

Thank you very much. It worked