You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: NEWS.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -168,7 +168,7 @@ Library improvements
168
168
169
169
* Data-structure processing
170
170
171
-
* New multidimensional iterators and index types for efficient iteration over `AbstractArray`s ([#8432]).
171
+
* New multidimensional iterators and index types for efficient iteration over `AbstractArray`s. Array iteration should generally be written as `for i in eachindex(A) ... end` rather than `for i = 1:length(A) ... end`. ([#8432])
172
172
173
173
* New implementation of SubArrays with substantial performance and functionality improvements ([#8501]).
indexed with scalars are dropped. For example, the dimensions of ``A[I, 1]`` will be
233
234
``(length(I),)``. Boolean vectors are first transformed with ``find``; the size of
@@ -236,6 +237,13 @@ As a special part of this syntax, the ``end`` keyword may be used to represent t
236
237
index of each dimension within the indexing brackets, as determined by the size of the
237
238
innermost array being indexed.
238
239
240
+
Alternatively, single elements of a multidimensional array can be indexed as
241
+
::
242
+
x = A[I]
243
+
244
+
where ``I`` is a ``CartesianIndex``, effectively an ``n``-tuple of integers.
245
+
See :ref:`man-array-iteration` below.
246
+
239
247
Indexing syntax is equivalent to a call to ``getindex``::
240
248
241
249
X = getindex(A, I_1, I_2, ..., I_n)
@@ -275,7 +283,7 @@ The general syntax for assigning values in an n-dimensional array A is::
275
283
276
284
A[I_1, I_2, ..., I_n] = X
277
285
278
-
where each I\_k may be:
286
+
where each ``I_k`` may be:
279
287
280
288
1. A scalar value
281
289
2. A ``Range`` of the form ``:``, ``a:b``, or ``a:b:c``
@@ -313,6 +321,49 @@ Example:
313
321
2 -1 -1
314
322
3 6 9
315
323
324
+
.. _man-array-iteration:
325
+
326
+
Iteration
327
+
---------
328
+
329
+
The recommended ways to iterate over a whole array are
330
+
::
331
+
332
+
for a in A
333
+
# Do something with the element a
334
+
end
335
+
336
+
for i in eachindex(A)
337
+
# Do something with i and/or A[i]
338
+
end
339
+
340
+
The first construct is used when you need the value, but not index, of each element. In the second construct, ``i`` will be an ``Int`` if ``A`` is an array
341
+
type with fast linear indexing; otherwise, it will be a ``CartesianIndex``::
342
+
343
+
A = rand(4,3)
344
+
B = sub(A, 1:3, 2:3)
345
+
julia> for i in eachindex(B)
346
+
@show i
347
+
end
348
+
i = Base.IteratorsMD.CartesianIndex_2(1,1)
349
+
i = Base.IteratorsMD.CartesianIndex_2(2,1)
350
+
i = Base.IteratorsMD.CartesianIndex_2(3,1)
351
+
i = Base.IteratorsMD.CartesianIndex_2(1,2)
352
+
i = Base.IteratorsMD.CartesianIndex_2(2,2)
353
+
i = Base.IteratorsMD.CartesianIndex_2(3,2)
354
+
355
+
In contrast with ``for i = 1:length(A)``, iterating with ``eachindex`` provides an efficient way to iterate over any array type.
356
+
357
+
Array traits
358
+
------------
359
+
360
+
If you write a custom ``AbstractArray`` type, you can specify that it has fast linear indexing using
This setting will cause ``eachindex`` iteration over a ``MyArray`` to use integers. If you don't specify this trait, the default value ``LinearSlow()`` is used.
0 commit comments