Hi @tk3369, thanks for the blog post. I is really useful.
But still I am unable to find why that specific error is occurring for he second version. It happens because I think there is some ambiguity about the size of I. But I would also assume that it could be easily found as in the case of non-dot version.
For normal usage, the I operator adapts its size to the context where it is used in. Take this as an example:
julia> A = rand(1:10, 2, 2) #get a simple matrix
2×2 Array{Int64,2}:
6 5
8 6
julia> [A; I] #extend the marix with the identity Matrix
4×2 Array{Int64,2}:
6 5
8 6
1 0
0 1
Here you can see, how the size of the identity operator I automatically matches the dimension of the matrix A.
In your case, you try to add a random number to each element of I. That fails because I has no idea about it’s size. To fix this, you just need to add the dimension like this:
Note that rand() .+ I(n) adds the same random number to all elements. If you want to add a different random number to each element of I(n) broadcast also the call to rand():
The normal version isn’t finding a size. I+rand() increments all diagonal elements by the same amount, so you don’t need a size. you are just calling rand once.
That’s just because I is being adapted to the size of the other input in the addition, which is just a number. When broadcasting, both inputs must have a definite size.