# \#cartesianindices

**URL:** https://discourse.julialang.org/tag/cartesianindices/847.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Find n smallest values in an n dims array](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092)

<div class="topic-metadata">

**Author:** [@marianoarnaiz](https://discourse.julialang.org/u/marianoarnaiz)\
**Replies:** 22\
**Last updated:** [February 13, 2026, 2:07pm UTC](https://discourse.julialang.org/t/find-n-smallest-values-in-an-n-dims-array/81092 "2026-02-13T14:07:33Z")

</div>

Hi Every one! I have the bad tendency of working with N dims arrays. Right now I have a 10x20x6 arrays and I need to find the index of the 5 smallest values. I have tried PartialQuickSort and argmin but I can’t get the…

---

## [Converting cartesian index to what it would be in view](https://discourse.julialang.org/t/converting-cartesian-index-to-what-it-would-be-in-view/135310)

<div class="topic-metadata">

**Author:** [@croberts](https://discourse.julialang.org/u/croberts)\
**Replies:** 4\
**Last updated:** [January 29, 2026, 10:37pm UTC](https://discourse.julialang.org/t/converting-cartesian-index-to-what-it-would-be-in-view/135310 "2026-01-29T22:37:01Z")

</div>

Suppose we have a = CartesianIndices((3,3)) b = a\[2:3, :\] indx = CartesianIndex((3,3)) map\_index\_to\_what\_it\_would\_be\_in\_slice(?, indx::CartesianIndex) = ? using Test @test a\[indx\] == b\[map\_index\_to\_what\_it\_would\_be…

---

## [Cartesian Indices Sequence on the GPU](https://discourse.julialang.org/t/cartesian-indices-sequence-on-the-gpu/134998)

<div class="topic-metadata">

**Author:** [@Chrysoberyl](https://discourse.julialang.org/u/Chrysoberyl)\
**Replies:** 0\
**Last updated:** [January 12, 2026, 8:24am UTC](https://discourse.julialang.org/t/cartesian-indices-sequence-on-the-gpu/134998 "2026-01-12T08:24:05Z")

</div>

I have an array of integers i, e.g. \[3,1,2\], and I want to map them to Cartesian indices \[(1, 3), (2, 1), (3, 2)\]. On the CPU, the easy way of creating this array is CartesianIndex.(enumerate(i)). However this does not …

---

## [CartesianIndex to an integer vector](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338)

<div class="topic-metadata">

**Author:** [@seismobassoon](https://discourse.julialang.org/u/seismobassoon)\
**Replies:** 7\
**Last updated:** [March 26, 2025, 2:11pm UTC](https://discourse.julialang.org/t/cartesianindex-to-an-integer-vector/127338 "2025-03-26T14:11:06Z")

</div>

I have a simple question, rather esthetic than essential: I need to use CartesianIndex as an integer vector. The code below does work with “collect+Tuple” but collect, Vector alone did not. Do you have any idea? julia\> …

---

## [Mod1 for CartesianIndex](https://discourse.julialang.org/t/mod1-for-cartesianindex/125065)

<div class="topic-metadata">

**Author:** [@JoergF](https://discourse.julialang.org/u/JoergF)\
**Replies:** 6\
**Last updated:** [January 22, 2025, 6:00pm UTC](https://discourse.julialang.org/t/mod1-for-cartesianindex/125065 "2025-01-22T18:00:47Z")

</div>

Since there is no implementation of mod1 for a CartesianIndex I wanted to implement one myself. However I am confused how to make it performant. function mod1(CI::CartesianIndex{2}, MOD::CartesianIndex{2}) Cartesian…

---

## [How to increment CartesianIndex?](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794)

<div class="topic-metadata">

**Author:** [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Replies:** 4\
**Last updated:** [March 18, 2024, 10:39pm UTC](https://discourse.julialang.org/t/how-to-increment-cartesianindex/111794 "2024-03-18T22:39:46Z")

</div>

Hello! I think I am just not finding the right command, but how to increment a CartesianIndex? I have a matrix of size 5x5. Suppose I have a cartesian index of CartesianIndex(3,1) how to increment it to CartesianIndex(4…

---

## [Matrix Free Differentiation](https://discourse.julialang.org/t/matrix-free-differentiation/107134)

<div class="topic-metadata">

**Author:** [@nicrummel](https://discourse.julialang.org/u/nicrummel)\
**Replies:** 7\
**Last updated:** [December 6, 2023, 10:36pm UTC](https://discourse.julialang.org/t/matrix-free-differentiation/107134 "2023-12-06T22:36:31Z")

</div>

Hello all, I am an Applied Mathematics PhD student at CU Boulder, and I want to implement fast matrix free finite difference methods using Julia. This appeared at first glance to be quite simple task, but when I compare…

---

## [Converting CartesianIndices to a vector of vectors](https://discourse.julialang.org/t/converting-cartesianindices-to-a-vector-of-vectors/102794)

<div class="topic-metadata">

**Author:** [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Replies:** 12\
**Last updated:** [August 15, 2023, 1:21am UTC](https://discourse.julialang.org/t/converting-cartesianindices-to-a-vector-of-vectors/102794 "2023-08-15T01:21:02Z")

</div>

I’m using CartesianIndices to generate lists resembling base-k numbers. I want the convert the output into a “flat” vector of vectors, irrespective of the number of “digits” passed to CartesianIndices. The lists will gen…

---

## [Is it possible use a Vector of CartesianIndex to select data on a matrix?](https://discourse.julialang.org/t/is-it-possible-use-a-vector-of-cartesianindex-to-select-data-on-a-matrix/102261)

<div class="topic-metadata">

**Author:** [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Replies:** 15\
**Last updated:** [July 30, 2023, 8:00pm UTC](https://discourse.julialang.org/t/is-it-possible-use-a-vector-of-cartesianindex-to-select-data-on-a-matrix/102261 "2023-07-30T20:00:13Z")

</div>

Hello, good afternoon. Is it possible use a Vector of CartesianIndex to select data on a matrix as below? The issue, its giving me an undefined result: Matrix{Float64}(undef, 0, 1) @show data\[groupidx .== 3, :\]

---

## [Change a vector CartesianIndex](https://discourse.julialang.org/t/change-a-vector-cartesianindex/94506)

<div class="topic-metadata">

**Author:** [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Replies:** 8\
**Last updated:** [February 13, 2023, 6:53pm UTC](https://discourse.julialang.org/t/change-a-vector-cartesianindex/94506 "2023-02-13T18:53:07Z")

</div>

I was wondering if I could get help with this problem. I have a matrix and find every index where its value is negative. mat = \[1 2 3 ; -1 -2 3\] ind = findall(\<(0),mat) 2-element Vector{CartesianIndex{2}}: Cartesian…

---

## [CartesianIndices performance: 3D faster than 2D?](https://discourse.julialang.org/t/cartesianindices-performance-3d-faster-than-2d/94110)

<div class="topic-metadata">

**Author:** [@lntricate](https://discourse.julialang.org/u/lntricate)\
**Replies:** 2\
**Last updated:** [February 6, 2023, 8:16pm UTC](https://discourse.julialang.org/t/cartesianindices-performance-3d-faster-than-2d/94110 "2023-02-06T20:16:52Z")

</div>

I have encountered a strange occurence when testing the performance of CartesianIndices: using BenchmarkTools function test(t::Tuple) for \_ ∈ CartesianIndices(t) end end @btime test((100,)) # 3.094 ns @btime test(…

---

## [How to get the Cartesian indices of a row/column in a \`Matrix\`?](https://discourse.julialang.org/t/how-to-get-the-cartesian-indices-of-a-row-column-in-a-matrix/91940)

<div class="topic-metadata">

**Author:** [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Replies:** 3\
**Last updated:** [December 21, 2022, 2:22pm UTC](https://discourse.julialang.org/t/how-to-get-the-cartesian-indices-of-a-row-column-in-a-matrix/91940 "2022-12-21T14:22:24Z")

</div>

Given a Matrix m, setting its values is simple: m = rand(4, 4) m\[begin, :\] = 1 m\[:, end\] = 2 But how can we get the indices of its first row/last column of elements? I guess it is similar to this question: Eachindex fo…

---

## [Convert between indexing formats](https://discourse.julialang.org/t/convert-between-indexing-formats/91658)

<div class="topic-metadata">

**Author:** [@smartalecH](https://discourse.julialang.org/u/smartalecH)\
**Replies:** 2\
**Last updated:** [December 14, 2022, 10:50pm UTC](https://discourse.julialang.org/t/convert-between-indexing-formats/91658 "2022-12-14T22:50:59Z")

</div>

Julia’s eachindex function makes it really easy to iterate over multidimensional arrays. Sometimes, however, I need access to not just the sequential index as described in memory (an Int) but also the individual dimensio…

---

## [Cheapest way for creating a CartesianIndex](https://discourse.julialang.org/t/cheapest-way-for-creating-a-cartesianindex/89819)

<div class="topic-metadata">

**Author:** [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Replies:** 9\
**Last updated:** [November 6, 2022, 8:45am UTC](https://discourse.julialang.org/t/cheapest-way-for-creating-a-cartesianindex/89819 "2022-11-06T08:45:47Z")

</div>

Hi, I am trying to write a generic multidimensional algorithm where I need a random access to an ND Array. I need to define a CartesianIndex with computed coordinates and modify my array using this CI. Unfortunately i…

---

## [Confusion On Cartesian And Linear Index](https://discourse.julialang.org/t/confusion-on-cartesian-and-linear-index/89479)

<div class="topic-metadata">

**Author:** [@uje](https://discourse.julialang.org/u/uje)\
**Replies:** 4\
**Last updated:** [October 31, 2022, 6:09am UTC](https://discourse.julialang.org/t/confusion-on-cartesian-and-linear-index/89479 "2022-10-31T06:09:02Z")

</div>

I know cartesian index is to be of this nature: x\[i, j\] and linear index is of x\[i\]. With that said, let me explain my confusion. I have a 3D array: julia\> A = \[1 2 3; 4 5 6;;; 7 8 9; 10 12 13\] 2×3×2 Array{Int64, 3}: \[…

---

## [How to output selective elements from Matrix?](https://discourse.julialang.org/t/how-to-output-selective-elements-from-matrix/85110)

<div class="topic-metadata">

**Author:** [@Amro](https://discourse.julialang.org/u/Amro)\
**Replies:** 11\
**Last updated:** [August 2, 2022, 7:29pm UTC](https://discourse.julialang.org/t/how-to-output-selective-elements-from-matrix/85110 "2022-08-02T19:29:36Z")

</div>

I have the below configuration. I am trying to output selective elements from matrix L.sl based on values of L.Seg. More specifically, I want to write syntax such as L.sl\[L.Seg\] to give me the same results as S. Any simp…

---

## [Heatmap from Matrix with custom indices](https://discourse.julialang.org/t/heatmap-from-matrix-with-custom-indices/81596)

<div class="topic-metadata">

**Author:** [@ctrebbau](https://discourse.julialang.org/u/ctrebbau)\
**Replies:** 20\
**Last updated:** [May 25, 2022, 5:13pm UTC](https://discourse.julialang.org/t/heatmap-from-matrix-with-custom-indices/81596 "2022-05-25T17:13:20Z")

</div>

Hello Community! I have data like so where I would like to visualise a heatmap of the TimeDiff\_mean column and the other two columns (representing a reticulation over geographical datapoints). I have sought for one …

---

## [Performance of using CartesianIndices with MArray](https://discourse.julialang.org/t/performance-of-using-cartesianindices-with-marray/77167)

<div class="topic-metadata">

**Author:** [@nchisholm](https://discourse.julialang.org/u/nchisholm)\
**Replies:** 2\
**Last updated:** [March 2, 2022, 3:29pm UTC](https://discourse.julialang.org/t/performance-of-using-cartesianindices-with-marray/77167 "2022-03-02T15:29:10Z")

</div>

Multidimensional algorithms built on CartesianIndices seem to have significant performance overhead when applied to StaticArrays.MArray​s, but not regular Array​s. Writing several functions with nested loops for differe…

---

## [Pluggin a series of small matrices into a larger matrix (with addition)](https://discourse.julialang.org/t/pluggin-a-series-of-small-matrices-into-a-larger-matrix-with-addition/74255)

<div class="topic-metadata">

**Author:** [@drew](https://discourse.julialang.org/u/drew)\
**Replies:** 1\
**Last updated:** [January 8, 2022, 8:51pm UTC](https://discourse.julialang.org/t/pluggin-a-series-of-small-matrices-into-a-larger-matrix-with-addition/74255 "2022-01-08T20:51:30Z")

</div>

What is the best way to add a series of smaller matrices to a particular point of a bigger matrix? For example: Starting with an empty matrix of zeros: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I would like to insert the fo…

---

## [Is there a findmin(A) that uses local search instead of visiting all elements of matrix A?](https://discourse.julialang.org/t/is-there-a-findmin-a-that-uses-local-search-instead-of-visiting-all-elements-of-matrix-a/74045)

<div class="topic-metadata">

**Author:** [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Replies:** 8\
**Last updated:** [January 5, 2022, 7:29am UTC](https://discourse.julialang.org/t/is-there-a-findmin-a-that-uses-local-search-instead-of-visiting-all-elements-of-matrix-a/74045 "2022-01-05T07:29:47Z")

</div>

I have a large matrix that contains sampled values of a convex 2D function. I want to find the minimum efficiently without visiting all elements, given indexes for a good starting guess close to the minimum. For example…

---

## [Seemingly unnecessary allocation within for loop](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556)

<div class="topic-metadata">

**Author:** [@mjg](https://discourse.julialang.org/u/mjg)\
**Replies:** 15\
**Last updated:** [December 10, 2021, 9:29pm UTC](https://discourse.julialang.org/t/seemingly-unnecessary-allocation-within-for-loop/72556 "2021-12-10T21:29:15Z")

</div>

I’m trying to create a simulation of an elastic string generalized to an arbitrary number of dimensions. To do so I track position and momentum (with an extra array to store the intermediate positions), and use the Euler…

---

## [How to use CartesianIndices to replace two nested loops](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034)

<div class="topic-metadata">

**Author:** [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Replies:** 5\
**Last updated:** [November 26, 2021, 6:14pm UTC](https://discourse.julialang.org/t/how-to-use-cartesianindices-to-replace-two-nested-loops/72034 "2021-11-26T18:14:44Z")

</div>

I’m trying to make a scatter plot where the x,y pairs form a (not necessarily rectangular) grid. The color of each point depends in a rather complicated way on the index of the point. Given two vectors, a1 and a2, my poi…

---

## [How can I easily skip indices in an array?](https://discourse.julialang.org/t/how-can-i-easily-skip-indices-in-an-array/70317)

<div class="topic-metadata">

**Author:** [@amrods](https://discourse.julialang.org/u/amrods)\
**Replies:** 16\
**Last updated:** [November 17, 2021, 6:40am UTC](https://discourse.julialang.org/t/how-can-i-easily-skip-indices-in-an-array/70317 "2021-11-17T06:40:19Z")

</div>

I have arrays with 2 dimensions. Elements in the arrays are only considered valid for certain values on the indices. For example, consider the arrays A and B below: A = round.(rand(10, 2) .\* 100) A\[1:3, 2\] .= NaN B = ro…

---

## [Sample rows of a 3 dimensional array without replacement](https://discourse.julialang.org/t/sample-rows-of-a-3-dimensional-array-without-replacement/71116)

<div class="topic-metadata">

**Author:** [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Replies:** 7\
**Last updated:** [November 8, 2021, 8:54am UTC](https://discourse.julialang.org/t/sample-rows-of-a-3-dimensional-array-without-replacement/71116 "2021-11-08T08:54:09Z")

</div>

I would like to sample rows from a three dimensional array without replacement. For example, if I have x = rand(10, 5, 3) a random set of indices might look like {(2,2), (9,3)}, where the first position is the index of…

---

## [Creating CartesianIndex](https://discourse.julialang.org/t/creating-cartesianindex/70721)

<div class="topic-metadata">

**Author:** [@sransome](https://discourse.julialang.org/u/sransome)\
**Replies:** 3\
**Last updated:** [November 2, 2021, 4:56pm UTC](https://discourse.julialang.org/t/creating-cartesianindex/70721 "2021-11-02T16:56:59Z")

</div>

Hi, I’m creating cartesianindexes for an 8 dimensional matrix using something like mypoint = indexin(1.0f0,mybigmatrix) This works fine and gives me something like … ‘’‘0-dimensional Array{Union{Nothing, CartesianInd…

---

## [Operations on Arrays of CartesianIndex](https://discourse.julialang.org/t/operations-on-arrays-of-cartesianindex/58827)

<div class="topic-metadata">

**Author:** [@ethomag](https://discourse.julialang.org/u/ethomag)\
**Replies:** 2\
**Last updated:** [April 9, 2021, 5:01am UTC](https://discourse.julialang.org/t/operations-on-arrays-of-cartesianindex/58827 "2021-04-09T05:01:06Z")

</div>

I came across a problem that I found surprisingly hard, given Julias excellent support for indexing multidimensional Arrays. I lilke to apply some function along one axis on parts of an Array. The parts are defined by s…

---

## [CartesianIndex() - how to? - general questions](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630)

<div class="topic-metadata">

**Author:** [@neo\_abs](https://discourse.julialang.org/u/neo_abs)\
**Replies:** 5\
**Last updated:** [August 27, 2020, 5:37pm UTC](https://discourse.julialang.org/t/cartesianindex-how-to-general-questions/45630 "2020-08-27T17:37:20Z")

</div>

Is there a convenient way to: Convert CaresianIndex into Vector{\<:Integer} Convert CartesianIndex{4} into CartesianIndex{3} by removing last coordinate? get k first values of Cartesian Index ( CI\[1:4\] → returns Cartes…

---

## [CartesianIndex to Arrays of index](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496)

<div class="topic-metadata">

**Author:** [@yusri-dh](https://discourse.julialang.org/u/yusri-dh)\
**Replies:** 6\
**Last updated:** [April 26, 2019, 7:43am UTC](https://discourse.julialang.org/t/cartesianindex-to-arrays-of-index/23496 "2019-04-26T07:43:21Z")

</div>

Dear all, I have some problems with CartesianIndex. In numpy when I want to index of nonzero elements I can do like this: \>\>\> x = np.array(\[\[3, 0, 0\], \[0, 4, 0\], \[5, 6, 0\]\]) \>\>\> x array(\[\[3, 0, 0\], \[0, 4, 0\], …

---

## [Iterators.product is not type stable?](https://discourse.julialang.org/t/iterators-product-is-not-type-stable/14481)

<div class="topic-metadata">

**Author:** [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Replies:** 20\
**Last updated:** [September 9, 2018, 5:28pm UTC](https://discourse.julialang.org/t/iterators-product-is-not-type-stable/14481 "2018-09-09T17:28:59Z")

</div>

I have a problem using product. I need a Cartesian product iterator but when I try using product it seems not type-stable as the following shows (Julia 1.0) julia\> m=\[1:2,3:5\] 2-element Array{UnitRange{Int64},1}: 1:2 …
