# How to convert CartesianIndex{N} values to Int64

**URL:** <https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 17, 2018, 3:54pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074 "2018-09-17T15:54:47Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomoyuki\_Kosaka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomoyuki_kosaka/32/5398_2.png) [@Tomoyuki\_Kosaka](https://discourse.julialang.org/u/Tomoyuki_Kosaka)\
**Post date:** [September 17, 2018, 3:54pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/1 "2018-09-17T15:54:47Z")

</div>

I’m using Clustering Pkg and function fuzzy\_cmean, and try to assign the cluster number to each data.  
If I use `findmax(FuzzyCMeansResult.weights, dims=2)` will give us (value, CartesianIndex(2)).  
Then, I cannot find out how to use `CartesianIndex(2)` values for real data.

For example, sometimes it will give us:  
CartesianIndex(1, 20)  
CartesianIndex(2, 19)  
…  
CartesianIndex(601, 19)  
CartesianIndex(602, 11)

I hope to use CartesianIndex(2) 2nd value for candidate cluster for each data.  
So, how to convert CartesianIndex values to Int64?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 17, 2018, 4:10pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/2 "2018-09-17T16:10:16Z")

</div>

[Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) point 4 is applicable here.

---

<div class="post-metadata">

**Author:** ![Tomoyuki\_Kosaka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomoyuki_kosaka/32/5398_2.png) [@Tomoyuki\_Kosaka](https://discourse.julialang.org/u/Tomoyuki_Kosaka)\
**Post date:** [September 18, 2018, 1:40am UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/3 "2018-09-18T01:40:28Z")

</div>

I considered and revised description. If I need to do more, please tell it to me.  
Thank you.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 18, 2018, 2:46am UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/4 "2018-09-18T02:46:29Z")

</div>

It would be nice to have a simple example that illustrates the problem and that doesn’t rely on any external packages. Something that can be copy pasted into the REPL.

A `CartesianIndex` represents an index into a potentially multidimensional array. So I’m not sure what converting it to an integer means. Do you mean how to convert it to a “linear index”?

```julia
julia> a = rand(2,2)
2×2 Array{Float64,2}:
 0.57097 0.0647051
 0.767868 0.531104

julia> I = LinearIndices(a)
2×2 LinearIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}:
 1 3
 2 4

julia> I[CartesianIndex(1,2)]
3

```

---

<div class="post-metadata">

**Author:** ![Tomoyuki\_Kosaka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomoyuki_kosaka/32/5398_2.png) [@Tomoyuki\_Kosaka](https://discourse.julialang.org/u/Tomoyuki_Kosaka)\
**Post date:** [September 18, 2018, 9:13am UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/5 "2018-09-18T09:13:07Z")

</div>

Thank you for your suggestion.  
I do not mention about “linear index”. I hope to extract index value itself.

> julia\> I[CartesianIndex(1,2)]  
> 3

I hope to extract “2” from CartesianIndex(1,2).  
How can I do it?

This is my output when doing the analysis.

```julia
julia> fuzzy_clusters = cluster_list[2]
645×1 Array{CartesianIndex{2},2}:
 CartesianIndex(1, 25)  
 CartesianIndex(2, 16)  
 CartesianIndex(3, 40)  
 CartesianIndex(4, 37)  
 CartesianIndex(5, 19)  
 CartesianIndex(6, 17)  
 CartesianIndex(7, 14)  
 CartesianIndex(8, 16)  
 CartesianIndex(9, 15)  
 CartesianIndex(10, 24) 
 CartesianIndex(11, 24) 
 CartesianIndex(12, 5)  
 CartesianIndex(13, 17) 
 ⋮                      
 CartesianIndex(634, 15)
 CartesianIndex(635, 25)
 CartesianIndex(636, 23)
 CartesianIndex(637, 40)
 CartesianIndex(638, 37)
 CartesianIndex(639, 23)
 CartesianIndex(640, 16)
 CartesianIndex(641, 14)
 CartesianIndex(642, 14)
 CartesianIndex(643, 13)
 CartesianIndex(644, 25)
 CartesianIndex(645, 36)

```

I hope extract the values “25, 16, 40, 37…,14, 13, 25, 36”.  
If I get them into Array or Tuple, then I can make a new column for my DataFrame.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 18, 2018, 10:13am UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/6 "2018-09-18T10:13:07Z")

</div>

> [@Tomoyuki\_Kosaka](#):
>
> I hope to extract “2” from CartesianIndex(1,2).  
> How can I do it?

Just extract it with normal indexing?

```julia
julia> CartesianIndex(1,20)[2]
20

```

---

<div class="post-metadata">

**Author:** ![Tomoyuki\_Kosaka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomoyuki_kosaka/32/5398_2.png) [@Tomoyuki\_Kosaka](https://discourse.julialang.org/u/Tomoyuki_Kosaka)\
**Post date:** [September 18, 2018, 1:59pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/7 "2018-09-18T13:59:39Z")

</div>

Thank you!  
I got it.

`[i[2] for i in fuzzy_clusters ]` or `map(i->i[2], fuzzy_clusters)`

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [October 13, 2019, 12:14pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/8 "2019-10-13T12:14:27Z")

</div>

Let’s say yo have 2 dimensional array `mA = randn(10, 2)`.  
I’d like array of the indices of its minimum argument along the rows:

```julia
vMinIdx = argmin(mA, dims = 1);

```

In the case above `vMinIdx` is a `2-element Array{CartesianIndex{2},1}:`.  
I intend to ask which row index has the value of `jj`. So I did it as following:

```julia
vMinIdx = map(ii -> ii[2], argmin(mA, dims = 1));

```

Then I do something like `sum(mX[:, vMinIdx .== jj], dims = 2);`.

Is that the most efficient way to do so? I just copied @GunnarFarneback [code](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/6) as we have the same issue.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 13, 2019, 9:09pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/9 "2019-10-13T21:09:10Z")

</div>

> [@RoyiAvital](#):
>
> ```julia
> sum(mX[:, vMinIdx .== jj], dims = 2)
> 
> ```

It’s unclear to me what you’re trying to do, since your code doesn’t run

```julia
ERROR: UndefVarError: jj not defined

```

Also, you want the minimum argument along the rows, but on the next line you write `dims=1`, which is along the columns.

Can you clarify, at least by providing runnable code?

Is what you are looking for one of the following alternatives:

```julia
julia> argmin.(eachcol(mA))
2-element Array{Int64,1}:
 2
 7

julia> argmin.(eachrow(mA))
10-element Array{Int64,1}:
 1
 1
 1
 1
 1
 1
 2
 2
 2
 2

```

?

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [October 13, 2019, 9:20pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/10 "2019-10-13T21:20:05Z")

</div>

Well, When I say along the Rows I mean the output will be row index.  
I guess I was sloppy with that. Sorry.

Basically what I’m after is something equivalent to MATLAB’s:

```matlab
[~, vMinRowIdx] = min(mA, [], 1);

```

So basically `vMinRowIdx` will be a vector holding the row index of the minimum value in each column of `mA`. I want to use this in a loop over `jj` which does (Again MATLAB code):

```julia
sum(mX(:, vMinIdx .== jj), 2);

```

You may have a look at [https://github.com/RoyiAvital/MatlabJuliaMatrixOperationsBenchmark/blob/47a52533c5533927e25c4c6e376d26490bc759f5/JuliaMatrixBenchmark0003.jl#L122](https://github.com/RoyiAvital/MatlabJuliaMatrixOperationsBenchmark/blob/47a52533c5533927e25c4c6e376d26490bc759f5/JuliaMatrixBenchmark0003.jl#L122).  
I wanted to know I’m doing it in the most efficient way (While being readable).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 13, 2019, 9:29pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/11 "2019-10-13T21:29:26Z")

</div>

> [@RoyiAvital](#):
>
> sum(mX[:, vMinIdx .== jj], dims = 2)

It would be good if you could test your code to check that it runs. I’ve spent a couple of minutes trying to fix it so that it runs, changing `mX` to `mA`, setting values for `jj`, etc. But it just fails. Can you provide tested, runnable code?

Also, it seems like `argmin.(eachcol(mA))` answers half of your question, at least.

(Unfortunately, I cannot look more at this tonight, though.)

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [October 13, 2019, 9:46pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/12 "2019-10-13T21:46:09Z")

</div>

I linked to the [code which is actually running](https://github.com/RoyiAvital/MatlabJuliaMatrixOperationsBenchmark/blob/47a52533c5533927e25c4c6e376d26490bc759f5/JuliaMatrixBenchmark0003.jl#L122):

```julia
function KMeansRunTime( matrixSize, mX )

  # Assuming Samples are slong Columns (Rows are features)
  numClusters = Int64(max(round(matrixSize / 100), 1));
  vClusterId = zeros(matrixSize);
  numIterations = 10;

  runTime = @elapsed begin
  # http://stackoverflow.com/questions/36047516/julia-generating-unique-random-integer-array
  mA = mX[:, randperm(matrixSize)[1:numClusters]]; #<! Cluster Centroids

  for ii = 1:numIterations
    vClusterId[:] = map(ii -> ii[2], argmin(sum(mA .^ 2, dims = 1)' .- (2 .* mA' * mX), dims = 1)); #<! Is there a `~` equivalent in Julia?
    for jj = 1:numClusters
      mA[:, jj] = sum(mX[:, vClusterId .== jj], dims = 2) ./ sum(vClusterId .== jj);
    end
  end

  end

```

Assume `mX` is a matrix of `matrixSize x matrixSize`.

For instance, it is equivalent (At least my equivalent) of [this MATLAB Code](https://github.com/RoyiAvital/MatlabJuliaMatrixOperationsBenchmark/blob/0ca4f4376f04eee6599a4b2e17255357fb888422/MatlabMatrixBenchmark0003.m#L140):

```matlab
function [mA, runTime] = KMeansRunTime( matrixSize, mX )

% Assuming Samples are slong Columns (Rows are features)
% mX = randn(matrixSize, matrixSize);
numClusters = max(round(matrixSize / 100), 1);
vClusterId = zeros(matrixSize, 1);
numIterations = 10;

tic();
mA = mX(:, randperm(matrixSize, numClusters)); %<! Cluster Centroids

for ii = 1:numIterations
    [~, vClusterId(:)] = min(sum(mA .^ 2, 1).' - (2 .* mA.' * mX), [], 1);
    for jj = 1:numClusters
        mA(:, jj) = sum(mX(:, vClusterId == jj), 2) ./ sum(vClusterId == jj);
    end
    
end
runTime = toc();

mA = mA(:, 1) + mA(:, end).';

  
end

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 13, 2019, 10:06pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/13 "2019-10-13T22:06:22Z")

</div>

The code you linked to was a large chunk of code that was difficult to identify with your specific question. And the code snippet you included in your latest post only returns runtime, as far as I can tell, and from just reading it also appears to be missing an `end`

Including a _tested minimal_ reproducible example, that can simply be cut and pasted will make everything much simpler. I don’t understand your resistance to this idea.

BTW, for benchmarking, you should wrap the code of interest in a function and then call it with `@benchmark`, rather than put `@elapsed` _inside_ the function.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [October 13, 2019, 10:26pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/14 "2019-10-13T22:26:02Z")

</div>

@DNF,

I created a file called `KMeans.jl` and this is its content:

```julia
using Random;

function KMeans()

    matrixSize = 10
    mX = randn(matrixSize, matrixSize)

    numClusters = Int64(max(round(matrixSize / 100), 1))
    vClusterId = zeros(matrixSize)
    numIterations = 10

    mA = mX[:, randperm(matrixSize)[1:numClusters]] #<! Cluster Centroids

    for ii = 1:numIterations
        vClusterId[:] = map(ii -> ii[2], argmin(sum(mA.^2, dims = 1)' .- (2 .* mA' * mX), dims = 1)) #<! Is there a `~` equivalent in Julia?
        for jj = 1:numClusters
            mA[:, jj] = sum(mX[:, vClusterId.==jj], dims = 2) ./ sum(vClusterId .== jj)
        end
    end

    return mA

end

```

I call it using `include("KMeans.jl");` and then run `mA = KMeans()`.  
I’m sorry if it is not elegant MWE. I am spoiled by MATLAB simplicity to create code and scoping.

Yet I think the reference should be what I do in MATLAB above.

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 16, 2022, 11:48am UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/15 "2022-03-16T11:48:03Z")

</div>

I have the same problem. From my own cluster-analysis code I get a vector with elements like;  
“CartesianIndex(3036,1)”  
I want a vector with elements only like 3036.

Would it be possible to change my code so that I get the simple integer output directly, without the need to extract from CartesianIndex output?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 16, 2022, 1:44pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/16 "2022-03-16T13:44:18Z")

</div>

Yes, I’m sure you can change your code to return what you want.

But without any more information, that’s all I can say. Could you open a new discussion thread and include more info about your problem? It’s best not to re-open old threads like this.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 16, 2022, 1:44pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/17 "2022-03-16T13:44:43Z")

</div>

That’s much easier to answer if you can provide a minimal working example of what you’re doing.

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 16, 2022, 3:29pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/18 "2022-03-16T15:29:59Z")

</div>

Also much easier said than done in this case.

My code that generates the output is 10 pages A4 long, so I’ll have to strip most of it to make it minimal, while making sure that it’s still working and producing the problematic output.  
But I will try.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [March 16, 2022, 4:13pm UTC](https://discourse.julialang.org/t/how-to-convert-cartesianindex-n-values-to-int64/15074/19 "2022-03-16T16:13:36Z")

</div>

To convert CartesianIndices to linear, you can do this:

```julia
julia> A = rand(1:10, 4, 4)
4×4 Matrix{Int64}:
 4 9 9 8
 2 9 6 1
 8 6 3 6
 1 3 9 7

julia> results = findlocalminima(A)
3-element Vector{CartesianIndex{2}}:
 CartesianIndex(2, 1)
 CartesianIndex(4, 1)
 CartesianIndex(2, 4)

julia> A[results]
3-element Vector{Int64}:
 2
 1
 1

julia> LinearIndices(A)[results]
3-element Vector{Int64}:
  2 # (2, 1) -> 2
  4 # (4, 1) -> 1
 14 # (2, 4) -> 1

```
