Skip to content

Commit fcd031b

Browse files
authored
Document more details on array assignment (#30092)
* Followup to #29167. More carefully explain the scalar case, and lead with the shape of the indices to drive the discussion
1 parent 55e95ad commit fcd031b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

doc/src/manual/arrays.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ julia> [(i,j) for i=1:3 for j=1:i if i+j == 4]
279279

280280
## [Indexing](@id man-array-indexing)
281281

282-
The general syntax for indexing into an n-dimensional array A is:
282+
The general syntax for indexing into an n-dimensional array `A` is:
283283

284284
```
285285
X = A[I_1, I_2, ..., I_n]
@@ -295,8 +295,8 @@ If all the indices are scalars, then the result `X` is a single element from the
295295
`X` is an array with the same number of dimensions as the sum of the dimensionalities of all the
296296
indices.
297297

298-
If all indices are vectors, for example, then the shape of `X` would be `(length(I_1), length(I_2), ..., length(I_n))`,
299-
with location `(i_1, i_2, ..., i_n)` of `X` containing the value `A[I_1[i_1], I_2[i_2], ..., I_n[i_n]]`.
298+
If all indices `I_k` are vectors, for example, then the shape of `X` would be `(length(I_1), length(I_2), ..., length(I_n))`,
299+
with location `i_1, i_2, ..., i_n` of `X` containing the value `A[I_1[i_1], I_2[i_2], ..., I_n[i_n]]`.
300300

301301
Example:
302302

@@ -364,9 +364,9 @@ julia> A[[1 2; 1 2], 1, 2, 1]
364364
5 6
365365
```
366366

367-
The location `(i_1, i_2, i_3, ..., i_{n+1})` contains the value at `A[I_1[i_1, i_2], I_2[i_3], ..., I_n[i_{n+1}]]`.
368-
All dimensions indexed with scalars are dropped. For example, the result of `A[2, I, 3]` is an
369-
array with size `size(I)`. Its `i`th element is populated by `A[2, I[i], 3]`.
367+
The location `i_1, i_2, i_3, ..., i_{n+1}` contains the value at `A[I_1[i_1, i_2], I_2[i_3], ..., I_n[i_{n+1}]]`.
368+
All dimensions indexed with scalars are dropped. For example, if `J` is an array of indices, then the result of `A[2, J, 3]` is an
369+
array with size `size(J)`. Its `j`th element is populated by `A[2, J[j], 3]`.
370370

371371
As a special part of this syntax, the `end` keyword may be used to represent the last index of
372372
each dimension within the indexing brackets, as determined by the size of the innermost array
@@ -410,7 +410,7 @@ julia> searchsorted(a, 4)
410410

411411
## Assignment
412412

413-
The general syntax for assigning values in an n-dimensional array A is:
413+
The general syntax for assigning values in an n-dimensional array `A` is:
414414

415415
```
416416
A[I_1, I_2, ..., I_n] = X
@@ -422,10 +422,18 @@ where each `I_k` may be a scalar integer, an array of integers, or any other
422422
ranges of the form `a:c` or `a:b:c` to select contiguous or strided
423423
subsections, and arrays of booleans to select elements at their `true` indices.
424424

425-
If `X` is an array, it must have the same number of elements as the product of the lengths of
426-
the indices: `prod(length(I_1), length(I_2), ..., length(I_n))`. The value in location `I_1[i_1], I_2[i_2], ..., I_n[i_n]`
427-
of `A` is overwritten with the value `X[i_1, i_2, ..., i_n]`. If `X` is a scalar, use the
428-
element-wise assignment operator `.=` to write the value to all referenced locations of `A`:
425+
If all indices `I_k` are integers, then the value in location `I_1, I_2, ..., I_n` of `A` is
426+
overwritten with the value of `X`, [`convert`](@ref)ing to the
427+
[`eltype`](@ref) of `A` if necessary.
428+
429+
430+
If any index `I_k` selects more than one location, then the right hand side `X` must be an
431+
array with the same shape as the result of indexing `A[I_1, I_2, ..., I_n]` or a vector with
432+
the same number of elements. The value in location `I_1[i_1], I_2[i_2], ..., I_n[i_n]` of
433+
`A` is overwritten with the value `X[I_1, I_2, ..., I_n]`, converting if necessary. The
434+
element-wise assignment operator `.=` may be used to [broadcast](@ref Broadcasting) `X`
435+
across the selected locations:
436+
429437

430438
```
431439
A[I_1, I_2, ..., I_n] .= X

0 commit comments

Comments
 (0)