1

I'm new to julia and I have a problem. I am working with Julia (Jupyter notebook) and I do not know how can I do column 3 - column 2 and write the result as a new column at the end of the matrix/array2D.

I have tried this: newCol = array[(1:end),3] - array[(1:end),2]

Any suggestion?

LBAlicia
  • 15
  • 6

1 Answers1

4

You can subtract the two columns and then concatenate it with the original array using the normal build-an-array syntax:

julia> arr
2x3 Array{Int32,2}:
 1  2  3
 5  6  7
julia> [arr [arr[:,3] - arr[:,2]]]
2x4 Array{Int32,2}:
 1  2  3  1
 5  6  7  1

Or use hcat:

julia> hcat(arr,arr[:,3] - arr[:,2])
2x4 Array{Int32,2}:
 1  2  3  1
 5  6  7  1

(Note that neither of these act in place, so you'd need to assign the result somewhere if you want to use it later.)

DSM
  • 342,061
  • 65
  • 592
  • 494