Fastest way to go from linear index to grid index
I'm sure this has been asked before but I'm interested in going from a position in a vector to the index in the grid version of the vector with given strides, for example, say I have the vector:
vec = {58, 94, 19, 68, 54, 77, 1, 18, 49, 20, 90, 44, 91, 89, 15, 0,
60, 18, 19, 44, 87, 5, 8, 42, 51, 55, 87, 71, 83, 68, 53, 58, 27,
17, 8, 14, 33, 58, 86, 3, 91, 66, 3, 16, 98, 84, 72, 98, 9, 30, 90,
99, 15, 0, 82, 76, 86, 58, 77, 58};
And say I have strides {5, 4, 3}
, the position 35
in the vector would correspond to the index {3, 4, 2}
:
vec[[35]]
4
ArrayReshape[vec, {5, 4, 3}][[3, 4, 2]]
4
How can I get this index fast and in a vectorized fashion because I will have potentially many positions to extract?
list-manipulation performance-tuning
add a comment |
I'm sure this has been asked before but I'm interested in going from a position in a vector to the index in the grid version of the vector with given strides, for example, say I have the vector:
vec = {58, 94, 19, 68, 54, 77, 1, 18, 49, 20, 90, 44, 91, 89, 15, 0,
60, 18, 19, 44, 87, 5, 8, 42, 51, 55, 87, 71, 83, 68, 53, 58, 27,
17, 8, 14, 33, 58, 86, 3, 91, 66, 3, 16, 98, 84, 72, 98, 9, 30, 90,
99, 15, 0, 82, 76, 86, 58, 77, 58};
And say I have strides {5, 4, 3}
, the position 35
in the vector would correspond to the index {3, 4, 2}
:
vec[[35]]
4
ArrayReshape[vec, {5, 4, 3}][[3, 4, 2]]
4
How can I get this index fast and in a vectorized fashion because I will have potentially many positions to extract?
list-manipulation performance-tuning
add a comment |
I'm sure this has been asked before but I'm interested in going from a position in a vector to the index in the grid version of the vector with given strides, for example, say I have the vector:
vec = {58, 94, 19, 68, 54, 77, 1, 18, 49, 20, 90, 44, 91, 89, 15, 0,
60, 18, 19, 44, 87, 5, 8, 42, 51, 55, 87, 71, 83, 68, 53, 58, 27,
17, 8, 14, 33, 58, 86, 3, 91, 66, 3, 16, 98, 84, 72, 98, 9, 30, 90,
99, 15, 0, 82, 76, 86, 58, 77, 58};
And say I have strides {5, 4, 3}
, the position 35
in the vector would correspond to the index {3, 4, 2}
:
vec[[35]]
4
ArrayReshape[vec, {5, 4, 3}][[3, 4, 2]]
4
How can I get this index fast and in a vectorized fashion because I will have potentially many positions to extract?
list-manipulation performance-tuning
I'm sure this has been asked before but I'm interested in going from a position in a vector to the index in the grid version of the vector with given strides, for example, say I have the vector:
vec = {58, 94, 19, 68, 54, 77, 1, 18, 49, 20, 90, 44, 91, 89, 15, 0,
60, 18, 19, 44, 87, 5, 8, 42, 51, 55, 87, 71, 83, 68, 53, 58, 27,
17, 8, 14, 33, 58, 86, 3, 91, 66, 3, 16, 98, 84, 72, 98, 9, 30, 90,
99, 15, 0, 82, 76, 86, 58, 77, 58};
And say I have strides {5, 4, 3}
, the position 35
in the vector would correspond to the index {3, 4, 2}
:
vec[[35]]
4
ArrayReshape[vec, {5, 4, 3}][[3, 4, 2]]
4
How can I get this index fast and in a vectorized fashion because I will have potentially many positions to extract?
list-manipulation performance-tuning
list-manipulation performance-tuning
asked 1 hour ago
b3m2a1
26.7k257154
26.7k257154
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I think this is what you want:
IntegerDigits[35 - 1, MixedRadix[{5, 4, 3}], 3] + 1
In general:
gridIndex[n_Integer, shape_List] :=
IntegerDigits[n - 1, MixedRadix[shape], Length@shape] + 1
1
@C.E. You're right, it just needs dimension length specification
– swish
38 mins ago
1
This is a very nice solution, +1
– C. E.
36 mins ago
add a comment |
Here's what I came up with:
getSubindex[index_, stride_] := {
Mod[index, stride, 1],
Ceiling[index/stride]
}
getIndex[index_, strides_] :=
Reverse@FoldPairList[getSubindex, index, Reverse@strides]
This is comparable to swish's solution speed-wise:
gridIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000061, {3, 2, 3, 4}}
getIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000052, {3, 2, 3, 4}}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188806%2ffastest-way-to-go-from-linear-index-to-grid-index%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think this is what you want:
IntegerDigits[35 - 1, MixedRadix[{5, 4, 3}], 3] + 1
In general:
gridIndex[n_Integer, shape_List] :=
IntegerDigits[n - 1, MixedRadix[shape], Length@shape] + 1
1
@C.E. You're right, it just needs dimension length specification
– swish
38 mins ago
1
This is a very nice solution, +1
– C. E.
36 mins ago
add a comment |
I think this is what you want:
IntegerDigits[35 - 1, MixedRadix[{5, 4, 3}], 3] + 1
In general:
gridIndex[n_Integer, shape_List] :=
IntegerDigits[n - 1, MixedRadix[shape], Length@shape] + 1
1
@C.E. You're right, it just needs dimension length specification
– swish
38 mins ago
1
This is a very nice solution, +1
– C. E.
36 mins ago
add a comment |
I think this is what you want:
IntegerDigits[35 - 1, MixedRadix[{5, 4, 3}], 3] + 1
In general:
gridIndex[n_Integer, shape_List] :=
IntegerDigits[n - 1, MixedRadix[shape], Length@shape] + 1
I think this is what you want:
IntegerDigits[35 - 1, MixedRadix[{5, 4, 3}], 3] + 1
In general:
gridIndex[n_Integer, shape_List] :=
IntegerDigits[n - 1, MixedRadix[shape], Length@shape] + 1
edited 35 mins ago
answered 47 mins ago
swish
3,9611534
3,9611534
1
@C.E. You're right, it just needs dimension length specification
– swish
38 mins ago
1
This is a very nice solution, +1
– C. E.
36 mins ago
add a comment |
1
@C.E. You're right, it just needs dimension length specification
– swish
38 mins ago
1
This is a very nice solution, +1
– C. E.
36 mins ago
1
1
@C.E. You're right, it just needs dimension length specification
– swish
38 mins ago
@C.E. You're right, it just needs dimension length specification
– swish
38 mins ago
1
1
This is a very nice solution, +1
– C. E.
36 mins ago
This is a very nice solution, +1
– C. E.
36 mins ago
add a comment |
Here's what I came up with:
getSubindex[index_, stride_] := {
Mod[index, stride, 1],
Ceiling[index/stride]
}
getIndex[index_, strides_] :=
Reverse@FoldPairList[getSubindex, index, Reverse@strides]
This is comparable to swish's solution speed-wise:
gridIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000061, {3, 2, 3, 4}}
getIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000052, {3, 2, 3, 4}}
add a comment |
Here's what I came up with:
getSubindex[index_, stride_] := {
Mod[index, stride, 1],
Ceiling[index/stride]
}
getIndex[index_, strides_] :=
Reverse@FoldPairList[getSubindex, index, Reverse@strides]
This is comparable to swish's solution speed-wise:
gridIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000061, {3, 2, 3, 4}}
getIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000052, {3, 2, 3, 4}}
add a comment |
Here's what I came up with:
getSubindex[index_, stride_] := {
Mod[index, stride, 1],
Ceiling[index/stride]
}
getIndex[index_, strides_] :=
Reverse@FoldPairList[getSubindex, index, Reverse@strides]
This is comparable to swish's solution speed-wise:
gridIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000061, {3, 2, 3, 4}}
getIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000052, {3, 2, 3, 4}}
Here's what I came up with:
getSubindex[index_, stride_] := {
Mod[index, stride, 1],
Ceiling[index/stride]
}
getIndex[index_, strides_] :=
Reverse@FoldPairList[getSubindex, index, Reverse@strides]
This is comparable to swish's solution speed-wise:
gridIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000061, {3, 2, 3, 4}}
getIndex[1000, {3, 5, 4, 6}] // RepeatedTiming
{0.000052, {3, 2, 3, 4}}
edited 17 mins ago
answered 38 mins ago
C. E.
49.9k397202
49.9k397202
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f188806%2ffastest-way-to-go-from-linear-index-to-grid-index%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown