Skip to content

Commit 9918a28

Browse files
committed
Followup to #29167.
More carefully explain the scalar case, and lead with the shape of the indices to drive the discussion
1 parent 1c5cde2 commit 9918a28

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

doc/src/manual/arrays.md

Lines changed: 14 additions & 7 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]
@@ -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,17 @@ 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`, [converting](@ref `convert`) to the [element type](@ref
427+
`eltype`) of `A` if necessary.
428+
429+
If any index `I_k` selects more than one location, then the right hand side `X` must be an
430+
array with the same shape as the result of indexing `A[I_1, I_2, ..., I_n]` or a vector with
431+
the same number of elements. The value in location `I_1[i_1], I_2[i_2], ..., I_n[i_n]` of
432+
`A` is overwritten with the value `X[I_1, I_2, ..., I_n]`, converting if necessary. The
433+
element-wise assignment operator `.=` may be used to [broadcast](@ref man-broadcasting) `X`
434+
across the selected locations:
435+
429436

430437
```
431438
A[I_1, I_2, ..., I_n] .= X
@@ -714,7 +721,7 @@ Also notice the difference between `max.(a,b)`, which [`broadcast`](@ref)s [`max
714721
elementwise over `a` and `b`, and [`maximum(a)`](@ref), which finds the largest value within
715722
`a`. The same relationship holds for `min.(a,b)` and `minimum(a)`.
716723

717-
## Broadcasting
724+
## [Broadcasting](@id man-broadcasting)
718725

719726
It is sometimes useful to perform element-by-element binary operations on arrays of different
720727
sizes, such as adding a vector to each column of a matrix. An inefficient way to do this would

0 commit comments

Comments
 (0)