# Array type modification

**URL:** <https://discourse.julialang.org/t/array-type-modification/13496>\
**Category:** New to Julia\
**Created:** [August 15, 2018, 12:41pm UTC](https://discourse.julialang.org/t/array-type-modification/13496 "2018-08-15T12:41:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 12:41pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/1 "2018-08-15T12:41:35Z")

</div>

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).

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

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 15, 2018, 12:45pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/2 "2018-08-15T12:45:51Z")

</div>

Please read [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 1:37pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/3 "2018-08-15T13:37:15Z")

</div>

Ok. It is changed.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 15, 2018, 1:47pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/4 "2018-08-15T13:47:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 15, 2018, 2:13pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/5 "2018-08-15T14:13:31Z")

</div>

> [@FiniteelementDuan](#):
>
> c2=a’\*b2

At least you can write:

```julia
c2 = a' * vec(b2)

```

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 2:14pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/6 "2018-08-15T14:14:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 2:18pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/7 "2018-08-15T14:18:42Z")

</div>

Thanks. It looks like a strange issue.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 15, 2018, 2:23pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/8 "2018-08-15T14:23:31Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 2:26pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/9 "2018-08-15T14:26:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 2:31pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/10 "2018-08-15T14:31:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 15, 2018, 2:45pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/12 "2018-08-15T14:45:43Z")

</div>

> [@FiniteelementDuan](#):
>
> 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).

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 15, 2018, 2:52pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/13 "2018-08-15T14:52:14Z")

</div>

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_.

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 3:07pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/14 "2018-08-15T15:07:11Z")

</div>

For one dimensional problems, I use:

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

```

For two dimensional problems, I use

```julia
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.

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 3:12pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/15 "2018-08-15T15:12:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 15, 2018, 3:31pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/16 "2018-08-15T15:31:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 3:53pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/17 "2018-08-15T15:53:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [August 15, 2018, 3:56pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/18 "2018-08-15T15:56:06Z")

</div>

> [@FiniteelementDuan](#):
>
> This could potentially lead to some problems in different applications.

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

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [August 15, 2018, 4:14pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/19 "2018-08-15T16:14:41Z")

</div>

> [@FiniteelementDuan](#):
>
> This would lead to the problem I posted originally, which is the return of a two dimensional array, rather than a number.

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

```julia
zeros(10, n_dim)

```

and then you just set `n_dim`.

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 4:17pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/20 "2018-08-15T16:17:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![FiniteelementDuan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/finiteelementduan/32/4972_2.png) [@FiniteelementDuan](https://discourse.julialang.org/u/FiniteelementDuan)\
**Post date:** [August 15, 2018, 4:22pm UTC](https://discourse.julialang.org/t/array-type-modification/13496/21 "2018-08-15T16:22:48Z")

</div>

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.

[Next page](https://discourse.julialang.org/t/array-type-modification/13496.md?page=2)
