2

So, I'm writing a function. In order to begin the computation, I need to create a vector of 1s. This is going to be multiplied by an inputted matrix A.

So if a matrix is 6x6, it becomes a vector of length 6, made up of 1s. If it's 4x4, it becomes a vector of length 4 with all values at 1.

How do I make the vector the right size for any given matrix A?

1 Answer 1

1

Suppose you have the following matrix A:

julia> A=reshape(1:9,3,3)
3×3 reshape(::UnitRange{Int64}, 3, 3) with eltype Int64:
 1  4  7
 2  5  8
 3  6  9

Than you can do:

julia> A*ones(size(A,1))
3-element Vector{Float64}:
 12.0
 15.0
 18.0

(If you rather meant a horizontal vector of ones you could do ones(1,size(A,1))*A)

Note that the same effect can be simply achieved with the dims parameter of the sum function:

julia> sum(A,dims=2)
3×1 Matrix{Int64}:
 12
 15
 18

or if you want a Vector:

julia> vec(sum(A,dims=2))
3-element Vector{Int64}:
 12
 15
 18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.