Array type modification

I have the following code, and it is not working, because c2 is a 1*1 adjoint float type array. If I define b2=ones(3), then the code will work. The question is that how to manually change the type of c2 ?(In my application, b2 is extracted from a matrix and couldn’t be defined).

using LinearAlgebra
a=[1.0,2,3]
b2=ones(3,1)
c2=a'*b2
det(c2)

Please read PSA: how to quote code with backticks

Ok. It is changed.

Can you explain a bit more of what you’re trying to achieve? c2 is a 1x1, so taking its determinant doesn’t seem very useful.

1 Like

At least you can write:

c2 = a' * vec(b2)

It is from a Jacobian matrix, and the dimension of the matrix could be 1x1, 2x2, or 3x3. I would like to keep the same form for all dimensions. The output I need is a number, rather than a 1x1 array.

One the other hand, a zeros(3) vector is different to a zeros(3,1) vector in terms of array type. This could lead to some problems in subsequent code development.

Thanks. It looks like a strange issue.

If that’s the case then I assume a will also be a matrix. The following works if a is a matrix.

julia> a = rand(3,2); b = rand(3, 2);

julia> a' * b
2×2 Array{Float64,2}:
 0.767651  0.615483
 0.478253  0.40591

julia> det(a' * b)
0.017240560608832924

Yes. It is only not working for one dimensional problems!

It seems that Julia is making zeros(3,1) as a two dimensional array, rather than a one dimensional vector. This seems to be a mistake for me.

This is intentional. Use zeros(3) for a vector (and you also get a vector when slicing a column from a matrix).

1 Like

I think the problem is the inconsistency of your use of vector vs 1 column matrix. a is a vector while b2 is a 1 column matrix. The multiplication of a row vector and a matrix can be either viewed as another row vector or as a matrix. In Julia, the “row vector”, i.e. Adjoint{Float64, Vector{Float64}} is chosen. The det of a “vector” doesn’t make sense, so if you want the result to be a matrix instead, just make a a matrix as well. On the other hand, if you define both as vectors, the result will be a scalar for which det is also defined. So really, just be consistent and it will just work.

1 Like

For one dimensional problems, I use:

a=zeros(10)
b=a[1:3,:]

For two dimensional problems, I use

c=zeros(10,2)
d=c[1:3,:]

You can see that a is a vector, however, b is a two dimensional array. For two dimensional problems, c and d are both two dimensional arrays.

I think here you can see the problem, I have to change the code for one and two dimensional problems. I am sure the same code will work in Matlab.

If you see the example I attached below, you will see the problem more clearly. I sliced a two dimensional array from a one dimensional vector!

Use zeros(10, 1) for one-dimensional problems if you want things to be consistent. You are creating a Vector in one case and a Matrix in the other.

This would lead to the problem I posted originally, which is the return of a two dimensional array, rather than a number.

I think the real issue is that the slice operation b=a[1:3,:] doesn’t work for vectors, because the output has become a two dimensional array. This could potentially lead to some problems in different applications.

Well, normally we wouldn’t use b=a[1:3,:] for vectors either.

An example would be appreciated. It seems that all problems showcased here can be worked out by just using matrices throughout.

You are saying you want your code to work in multiple dimensions but then you do something completely different when you are in 1 dimension. Those two things seems at odds to me.

What you seem to want is

zeros(10, n_dim)

and then you just set n_dim.

The problem I showed is that for one dimensional problems, you would normally only define vectors. You wouldn’t define matrices for a one dimensional problem.

If you define a matrix for a one dimensional problem, then it works. However, my point is that people normally only define a vector for a one dimensional problem, and then you could get matrices out from slicing a vector.
In my original example, a is a vector defined for a one dimensional problem. This would be a natural thing, and b2 is sliced from a vector, which would also be normal. It is not working in Julia.
Of course, after the original example, you know that vectors could lead to problems, and now you define matrices for even one dimensional problems, which is ok for me. But I think people don’t know this could still have problems.