# Transition matrix of multiple-dimensional state and sparse array

**URL:** https://discourse.julialang.org/t/transition-matrix-of-multiple-dimensional-state-and-sparse-array/100453
**Category:** New to Julia
**Tags:** question
**Created:** [June 16, 2023, 2:58pm UTC](https://discourse.julialang.org/t/transition-matrix-of-multiple-dimensional-state-and-sparse-array/100453 "2023-06-16T14:58:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 16, 2023, 2:58pm UTC](https://discourse.julialang.org/t/transition-matrix-of-multiple-dimensional-state-and-sparse-array/100453/1 "2023-06-16T14:58:57Z")

</div>

I was wondering if I could get some help with my problem.

Suppose I have a four-dimensional state space (x,y,z,w), and the number of grid points in each state space is (30,60,90,120). I have a function f:(x,y,z,w) \rightarrow (x,y,z,w) that returns a probability of moving from one state (x\_{1},y\_{1},z\_{1},w\_{1}), which is a vector of indices, to another (x\_{2},y\_{2},z\_{2},w\_{2}). For instance, if I pick (1,3,2,4), it means I’m taking my input of the first grid point in the x-space, the third in the y-space, and so on. I know that the function will return 0 for most cases, but I don’t know what the sparsity would look like.

The way that I’ve been thinking of making its transition matrix is as follows.

1. Make a square matrix 30\*60\*90\*120 \times 30\*60\*90\*120
2. Make a function that picks a coordinate that represents the transition from (x\_{1},y\_{1},z\_{1},w\_{1}) to (x\_{2},y\_{2},z\_{2},w\_{2})
3. Fill the coordinate with the value from the function f.

My questions are twofold.

1. Would there be a package that can simplify the second step? I would want to have a function that translate [[x\_1,y\_1,z\_1,w\_1],[x\_2,y\_2,z\_2,w\_2]] to a coordinate in the transition matrix. I think using the Kronecker product and the reshape function would be the way, but I’ve been stuck at this stage.

2. Ultimately, I want the inverse of the transition matrix, but the size is already huge. Ideally, I would want to use the SparseArrays. However, I’m uncertain how to make a vector of rows, cols, and vals related to my first concern.

Any suggestion would be more than helpful.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 16, 2023, 3:53pm UTC](https://discourse.julialang.org/t/transition-matrix-of-multiple-dimensional-state-and-sparse-array/100453/2 "2023-06-16T15:53:31Z")

</div>

> [@Zerosum](#):
>
> Ultimately, I want the inverse of the transition matrix, but the size is already huge. Ideally, I would want to use the SparseArrays.

The inverse of a sparse matrix is usually dense AFAIK. And the inverse is numerically unstable in general, so I don’t think the result would be accurate for your huge matrix. Perhaps some matrix decomposition would suit you better than the inverse? Maybe one of these:

> **[The Big Six Matrix Factorizations](https://nhigham.com/2022/05/18/the-big-six-matrix-factorizations/)**
>
> Six matrix factorizations dominate in numerical linear algebra and matrix analysis: for most purposes one of them is sufficient for the task at hand. We summarize them here. For each factorization …

---

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 16, 2023, 4:12pm UTC](https://discourse.julialang.org/t/transition-matrix-of-multiple-dimensional-state-and-sparse-array/100453/3 "2023-06-16T16:12:07Z")

</div>

Thank you very much!

I’ll check the post!
