Skip to content

Instantly share code, notes, and snippets.

@Lirimy
Created January 5, 2021 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lirimy/53dee64d39c5da9e568b1f7ad553962d to your computer and use it in GitHub Desktop.
Save Lirimy/53dee64d39c5da9e568b1f7ad553962d to your computer and use it in GitHub Desktop.
Cartesian index
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×3×4 CartesianIndices{3,Tuple{Base.OneTo{Int64},Base.OneTo{Int64},Base.OneTo{Int64}}}:\n",
"[:, :, 1] =\n",
" CartesianIndex(1, 1, 1) CartesianIndex(1, 2, 1) CartesianIndex(1, 3, 1)\n",
" CartesianIndex(2, 1, 1) CartesianIndex(2, 2, 1) CartesianIndex(2, 3, 1)\n",
"\n",
"[:, :, 2] =\n",
" CartesianIndex(1, 1, 2) CartesianIndex(1, 2, 2) CartesianIndex(1, 3, 2)\n",
" CartesianIndex(2, 1, 2) CartesianIndex(2, 2, 2) CartesianIndex(2, 3, 2)\n",
"\n",
"[:, :, 3] =\n",
" CartesianIndex(1, 1, 3) CartesianIndex(1, 2, 3) CartesianIndex(1, 3, 3)\n",
" CartesianIndex(2, 1, 3) CartesianIndex(2, 2, 3) CartesianIndex(2, 3, 3)\n",
"\n",
"[:, :, 4] =\n",
" CartesianIndex(1, 1, 4) CartesianIndex(1, 2, 4) CartesianIndex(1, 3, 4)\n",
" CartesianIndex(2, 1, 4) CartesianIndex(2, 2, 4) CartesianIndex(2, 3, 4)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"CIs = CartesianIndices((2, 3, 4))\n",
"\n",
"# 配列からも生成できる\n",
"#X = rand(2, 3, 4)\n",
"#CIs = CartesianIndices(X)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(i, j, k) = CI.I = (1, 1, 1)\n",
"(i, j, k) = CI.I = (2, 1, 1)\n",
"(i, j, k) = CI.I = (1, 2, 1)\n",
"(i, j, k) = CI.I = (2, 2, 1)\n",
"(i, j, k) = CI.I = (1, 3, 1)\n",
"(i, j, k) = CI.I = (2, 3, 1)\n",
"(i, j, k) = CI.I = (1, 1, 2)\n",
"(i, j, k) = CI.I = (2, 1, 2)\n",
"(i, j, k) = CI.I = (1, 2, 2)\n",
"(i, j, k) = CI.I = (2, 2, 2)\n",
"(i, j, k) = CI.I = (1, 3, 2)\n",
"(i, j, k) = CI.I = (2, 3, 2)\n",
"(i, j, k) = CI.I = (1, 1, 3)\n",
"(i, j, k) = CI.I = (2, 1, 3)\n",
"(i, j, k) = CI.I = (1, 2, 3)\n",
"(i, j, k) = CI.I = (2, 2, 3)\n",
"(i, j, k) = CI.I = (1, 3, 3)\n",
"(i, j, k) = CI.I = (2, 3, 3)\n",
"(i, j, k) = CI.I = (1, 1, 4)\n",
"(i, j, k) = CI.I = (2, 1, 4)\n",
"(i, j, k) = CI.I = (1, 2, 4)\n",
"(i, j, k) = CI.I = (2, 2, 4)\n",
"(i, j, k) = CI.I = (1, 3, 4)\n",
"(i, j, k) = CI.I = (2, 3, 4)\n"
]
}
],
"source": [
"# 逐次的に得られればよいなら\n",
"\n",
"for CI in CIs\n",
" @show i, j, k = CI.I\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"2×3×4 Array{Tuple{Int64,Int64,Int64},3}:\n",
"[:, :, 1] =\n",
" (1, 1, 1) (1, 2, 1) (1, 3, 1)\n",
" (2, 1, 1) (2, 2, 1) (2, 3, 1)\n",
"\n",
"[:, :, 2] =\n",
" (1, 1, 2) (1, 2, 2) (1, 3, 2)\n",
" (2, 1, 2) (2, 2, 2) (2, 3, 2)\n",
"\n",
"[:, :, 3] =\n",
" (1, 1, 3) (1, 2, 3) (1, 3, 3)\n",
" (2, 1, 3) (2, 2, 3) (2, 3, 3)\n",
"\n",
"[:, :, 4] =\n",
" (1, 1, 4) (1, 2, 4) (1, 3, 4)\n",
" (2, 1, 4) (2, 2, 4) (2, 3, 4)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# タプルの配列として得たいなら\n",
"\n",
"getproperty.(CIs, :I)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Julia 1.5.3",
"language": "julia",
"name": "julia-1.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment