Main Content

Subscripted assignment

`a(I) = b`

a(I,J) = b

a(I,:) = b

a(:,I)
= b

a(I,J,K,...) = b

a = subsasgn(a,S,b)

`a(I) = b`

assigns the values of `b`

into
the elements of `a`

specified by the subscript vector `I`

. `b`

must
have the same number of elements as `I`

or be a scalar
value.

`a(I,J) = b`

assigns the values of `b`

into
the elements of the rectangular submatrix of `a`

specified
by the subscript vectors `I`

and `J`

. `b`

must
have `LENGTH(I)`

rows and `LENGTH(J)`

columns.

A colon used as a subscript, as in `a(I,:) = b`

or ```
a(:,I)
= b
```

indicates the entire column or row.

For multidimensional arrays, `a(I,J,K,...) = b`

assigns `b`

to
the specified elements of `a`

. `b`

must
be `length(I)`

-by-`length(J)`

-by-`length(K)`

-...
or be shiftable to that size by adding or removing singleton dimensions.

`a = subsasgn(a,S,b)`

is called for the syntax `a(i)=b`

, `a{i}=b`

,
or `a.i=b`

when `a`

is an object. `S`

is
a structure array with the following fields:

type — One of the following:

`'()'`

,`'{}'`

, or`'.'`

specifying the subscript typesubs — Cell array or character vector containing the actual subscripts

For instance, the syntax `a(1:2,:) = b`

calls `a=subsasgn(a,S,b)`

where `S`

is
a 1-by-1 structure with `S.type='()'`

and ```
S.subs
= {1:2,':'}
```

. A colon used as a subscript is passed as `':'`

.

You can use fixed-point assignment, for example ```
a(:)
= b
```

, to cast a value with one `numerictype`

object
into another `numerictype`

object. This subscripted
assignment statement assigns the value of `b`

into `a`

while
keeping the `numerictype`

object of `a`

.
Subscripted assignment works the same way for integer data types.