# How to assign Matrix with dimensions (1,1) to a scalar

**URL:** https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808
**Category:** New to Julia
**Tags:** question, matlab, matrices
**Created:** [March 9, 2021, 2:17pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808 "2021-03-09T14:17:54Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tqml](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tqml/32/22710_2.png) [@tqml](https://discourse.julialang.org/u/tqml)
#### Post date: [March 9, 2021, 2:17pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/1 "2021-03-09T14:17:54Z")

</div>

Hello I’m quite new to Julia and coming from MATLAB and Python and I have some trouble with operations that _should_ be quite common but I haven’t found any workaround/solution in the usual places (Manual, Github and here).

The problem is:  
I cannot assign the result of a calculation to array at a specific index. The issue is that the type of the result is a Matrix with dimensions (1,1) which is cannot automatically converted to a scalar (why not?)

Example to reproduce:

```julia
A = rand(2,2)
B = rand(2,1)
C = rand(1,2)
r = C * A * B
y_ = zeros(10,1)
y_[1] = r

```

this results in:

```julia
MethodError: Cannot `convert` an object of type Array{Float64,2} to an object of type Float64

```

I know that I could just index `r[1]` but this also seems overly complicated.  
Are there any better solutions to this?  
Is it possible to overwrite e.g. `convert()`?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [March 9, 2021, 2:21pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/2 "2021-03-09T14:21:39Z")

</div>

Probably `only` is the best. That also assures that there is indeed only one element.

> Is it possible to overwrite e.g. `convert()` ?

No, don’t do that. This will probably break things as other code, in Julia itself or in packages will rely on `convert` working as advertised (this is called type piracy). Only add methods to `convert` for types which you defined.

---

<div class="post-metadata">

### Author: ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)
#### Post date: [March 9, 2021, 2:22pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/3 "2021-03-09T14:22:17Z")

</div>

You can use `r[]` (no indices, just the brackets).

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [March 9, 2021, 2:26pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/4 "2021-03-09T14:26:35Z")

</div>

The better answer is that you should be using `Vector`s instead of 1 column matrices. Specifically, this should be

```julia
A = rand(2,2) #matrix
B = rand(2) #vector
C = rand(2)' #row vector
r = C * A * B #scalar
y_ = zeros(10,1)
y_[1] = r

```

---

<div class="post-metadata">

### Author: ![tqml](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tqml/32/22710_2.png) [@tqml](https://discourse.julialang.org/u/tqml)
#### Post date: [March 9, 2021, 2:42pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/5 "2021-03-09T14:42:44Z")

</div>

Hmm, `only()` looks interesting thanks

So just to make sure I’m not missing something: In Julia a scalar are not equivalent and cannot be automatically casted to a vector/matrix with one element?

---

<div class="post-metadata">

### Author: ![tqml](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tqml/32/22710_2.png) [@tqml](https://discourse.julialang.org/u/tqml)
#### Post date: [March 9, 2021, 2:45pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/6 "2021-03-09T14:45:18Z")

</div>

hmm that’s good to know thanks  
(but it’s the same workaround like `[1]` I was trying to avoid)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [March 9, 2021, 2:48pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/7 "2021-03-09T14:48:25Z")

</div>

> [@tqml](#):
>
> So just to make sure I’m not missing something: In Julia a scalar are not equivalent and cannot be automatically casted to a vector/matrix with one element?

Yes. They are two completely different things. But as @Oscar_Smith pointed our above, if you use the “right” types for the rows and columns the result is a scalar.

---

<div class="post-metadata">

### Author: ![tqml](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tqml/32/22710_2.png) [@tqml](https://discourse.julialang.org/u/tqml)
#### Post date: [March 9, 2021, 2:51pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/8 "2021-03-09T14:51:11Z")

</div>

Yes that’s true, unfortunately it’s not always possible to constrain/control the types in that way. This example e.g. is derived from using `ControlSystems` package and there the system object automatically uses matrices.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [March 9, 2021, 2:55pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/9 "2021-03-09T14:55:31Z")

</div>

Looking at the documentation, it appears that you can use `Vector`s (in fact, most of the docs do)

---

<div class="post-metadata">

### Author: ![tqml](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tqml/32/22710_2.png) [@tqml](https://discourse.julialang.org/u/tqml)
#### Post date: [March 9, 2021, 3:38pm UTC](https://discourse.julialang.org/t/how-to-assign-matrix-with-dimensions-1-1-to-a-scalar/56808/10 "2021-03-09T15:38:01Z")

</div>

Hmm, ok makes sense from a programming point of view (not mathematically though in my opinion)

Anyway thank you all for the quick and helpful responses!
