# Matlab's sparse matrix function

**URL:** https://discourse.julialang.org/t/matlabs-sparse-matrix-function/36473
**Category:** General Usage
**Tags:** question, array, sparse
**Created:** [March 25, 2020, 1:29am UTC](https://discourse.julialang.org/t/matlabs-sparse-matrix-function/36473 "2020-03-25T01:29:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [March 25, 2020, 1:29am UTC](https://discourse.julialang.org/t/matlabs-sparse-matrix-function/36473/1 "2020-03-25T01:29:35Z")

</div>

In MATLAB it’s possible to use `sparse` like this:

```nohighlight
>> full(sparse([2 3 4 5; 1 2 3 4; 9 8 6 7],ones(3,1)*(1:4),1,9,9))

ans =

     1 0 0 0 0 0 0 0 0
     1 1 0 0 0 0 0 0 0
     0 1 1 0 0 0 0 0 0
     0 0 1 1 0 0 0 0 0
     0 0 0 1 0 0 0 0 0
     0 0 1 0 0 0 0 0 0
     0 0 0 1 0 0 0 0 0
     0 1 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0

```

Does julia support a similar constructor for sparse matrices? I realize I can make a loop to achieve the same effect, but I’m wondering if there is a built-in function somwhere.

Specifically, note that the input are matrices and not vectors.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [March 25, 2020, 4:46am UTC](https://discourse.julialang.org/t/matlabs-sparse-matrix-function/36473/2 "2020-03-25T04:46:01Z")

</div>

Does the above code in matlab not simply iterate through the first and second input values? I.e. does their dimensionality (vector/matrix) have any bearing on the output? If not, and turning them into vectors still yields what you want, you can simply `vec` the first two input arguments.

```julia
sparse(vec(I), vec(J), v, m, n)

```
