1

I'm attempting to create an array of matrices by multiplying a t = range(0,stop=2*pi,length=101) by a matrix [1, 0] as follows

A = t .* [1 ,0]

but this produces the error ERROR: LoadError: DimensionMismatch("arrays could not be broadcast to a common size"). I would like each scalar, or element of t to be multiplied elementwise (in terms of t) with the elements of the vector [1 , 0], essentially performing an elementwise scalar--matrix product.

The reason I'm doing this is because I would later like to be able to multiply another constant matrix M with each column vector found in A. How can this be done in Julia v1.1?

2
  • [1, 0] is not a matrix, it's a vector, so you're attempting to multiply two vectors of different lengths with each other, and that won't work. If you want a matrix, write [1 0] without a comma. But then the output of the multiplication will just be a matrix, not an array of matrices. Commented Aug 28, 2019 at 13:03
  • I guess the question asked for what I discuss in my answer (I am not 100% sure though). @SeSodesa - for a reference: matrix is always 2-dimensional array in Julia terminology. 1-dimensional arrays are called vectors. Commented Aug 28, 2019 at 13:08

1 Answer 1

1

You have to wrap the element you do not want to be broadcasted over in a container. Here is a standard way to do it (I decreased length kwarg to 3 to make the example more clear):

julia> t = range(0,stop=2*pi,length=3)
0.0:3.141592653589793:6.283185307179586

julia> A = t .* Ref([1 ,0])
3-element Array{Array{Float64,1},1}:
 [0.0, 0.0]
 [3.141592653589793, 0.0]
 [6.283185307179586, 0.0]

julia> Ref([1 2; 3 4]) .* A
3-element Array{Array{Float64,1},1}:
 [0.0, 0.0]
 [3.141592653589793, 9.42477796076938]
 [6.283185307179586, 18.84955592153876]

Instead of Ref container you can also use a 1-element tuple or 1-element vector as wrappers:

julia> t .* ([1 ,0],)
3-element Array{Array{Float64,1},1}:
 [0.0, 0.0]
 [3.141592653589793, 0.0]
 [6.283185307179586, 0.0]

julia> t .* [[1 ,0]]
3-element Array{Array{Float64,1},1}:
 [0.0, 0.0]
 [3.141592653589793, 0.0]
 [6.283185307179586, 0.0]

The reason why Ref should be preferred is that it is 0-dimensional, so that it is the most neutral of these three methods (i.e. influences the output in the least way - retaining the broadcast style of the other argument). Here are some examples:

julia> f1(x) = x .* (2, )
f1 (generic function with 1 method)

julia> f2(x) = x .* [2]
f2 (generic function with 1 method)

julia> f3(x) = x .* Ref(2)
f3 (generic function with 1 method)

julia> f1(1)
(2,)

julia> f2(1)
1-element Array{Int64,1}:
 2

julia> f3(1)
2

julia> f1((1,2))
(2, 4)

julia> f2((1,2))
2-element Array{Int64,1}:
 2
 4

julia> f3((1,2))
(2, 4)
Sign up to request clarification or add additional context in comments.

4 Comments

This is essentially what I was going for. Now I just have the issue that the resulting array contains a bunch of 1-dimensional arrays, and the matrix M I'm trying to multiply each of these with if a 2x2 matrix, so I'm once again faced with a dimension mismatch.
See my example above. I have shown you how to do it already. replace [1 2; 3 4] with M and you will get what you wanted. I am assuming that you want to do matrix multiplication (not broadcasted multiplication) - right?
I would like to do broadcasted matrix multiplication. So essentially A should contain 2x1 column vectors, and each of these should be multiplied (in the matrix sense) with M. The result should be a vector of of the matrix multiplication results, of equal length to A.
If you wanted broadcasted multiplication do [M .* v for v in A] or broadcast.(*, Ref(M), A). In general - please try asking the initial questions precisely (I know it is sometimes hard though :)) then it is easier to answer them the way you expect. If you want standard multiplication - do what I have suggested above Ref(M) .* A.

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.