python - replace multiple if elif statements and conditions with numpy function such as numpy.where
Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where
def radio_index2xy(radio_index,n_x,n_y):
con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)
return x_pos,y_pos
if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1
python numpy if-statement
add a comment |
Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where
def radio_index2xy(radio_index,n_x,n_y):
con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)
return x_pos,y_pos
if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1
python numpy if-statement
3
Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 '18 at 21:40
1
Also there's not a singleif/elifin this code.
– timgeb
Nov 10 '18 at 21:41
This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 '18 at 4:37
The if/elif method can be run.
– Yci Meow
Nov 11 '18 at 4:37
add a comment |
Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where
def radio_index2xy(radio_index,n_x,n_y):
con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)
return x_pos,y_pos
if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1
python numpy if-statement
Radio_index, n_x and n_y are integers
I wrote a if/elif code that can be run.
The purpose is to find x,y position for a radio_index
Am I able to do with np.where
def radio_index2xy(radio_index,n_x,n_y):
con1 = radio_index <= n_x
con2 = (radio_index > n_x) & (radio_index <= n_x+n_y-1)
con3 = (radio_index > n_x+n_y-1) & (radio_index <= 2*n_x+n_y-2)
con4 = (radio_index > 2*n_x+n_y-2) & (radio_index <= 2*n_x+2*n_y-4)
condlist = [[con1],[con2],[con3],[con4]]
choicelist = [[x_pos = radio_index -1 ,y_pos = 0],
[(x_pos = n_x -1),(y_pos = radio_index - n_x)],
[(x_pos = (n_x-1)-(radio_index-n_x-n_y+1)),(y_pos = n_y -1)],
[(x_pos = 0),(y_pos = 2*n_x+2*n_y-4-radio_index+1)]]
np.select(condlist,choicelist)
return x_pos,y_pos
if radio_index <= n_x:
x_pos = radio_index -1
y_pos = 0
elif radio_index > n_x and radio_index <= n_x+n_y-1:
x_pos = n_x -1
y_pos = radio_index - n_x
elif radio_index > n_x+n_y-1 and radio_index <= 2*n_x+n_y-2:
x_pos = (n_x-1)-(radio_index-n_x-n_y+1)
y_pos = n_y -1
elif radio_index > 2*n_x+n_y-2 and radio_index <= 2*n_x+2*n_y-4:
x_pos = 0
y_pos = 2*n_x+2*n_y-4-radio_index+1
python numpy if-statement
python numpy if-statement
edited Nov 11 '18 at 4:40
Yci Meow
asked Nov 10 '18 at 21:37
Yci MeowYci Meow
83
83
3
Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 '18 at 21:40
1
Also there's not a singleif/elifin this code.
– timgeb
Nov 10 '18 at 21:41
This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 '18 at 4:37
The if/elif method can be run.
– Yci Meow
Nov 11 '18 at 4:37
add a comment |
3
Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 '18 at 21:40
1
Also there's not a singleif/elifin this code.
– timgeb
Nov 10 '18 at 21:41
This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 '18 at 4:37
The if/elif method can be run.
– Yci Meow
Nov 11 '18 at 4:37
3
3
Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 '18 at 21:40
Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 '18 at 21:40
1
1
Also there's not a single
if/elif in this code.– timgeb
Nov 10 '18 at 21:41
Also there's not a single
if/elif in this code.– timgeb
Nov 10 '18 at 21:41
This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 '18 at 4:37
This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 '18 at 4:37
The if/elif method can be run.
– Yci Meow
Nov 11 '18 at 4:37
The if/elif method can be run.
– Yci Meow
Nov 11 '18 at 4:37
add a comment |
1 Answer
1
active
oldest
votes
np.searchsorted is useful for this kind of logic:
def radio_index2xy_v(radio_index, n_x, n_y):
sgn = np.array([0, 1, 1, -1, -1, 0])
col = np.array([-1, 1, 0, 1, 0, -1])
coeffs = np.array([[-1, -1],
[0, -1],
[-n_x, n_x - 1],
[n_y - 1, 2*n_x + n_y - 2],
[2*n_x + 2*n_y - 3, 0],
[-1, -1]])
cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
idx = cusps.searchsorted(radio_index)
out = coeffs[idx]
out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
return out
Demo:
>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 1, 4],
[ 2, 4],
[ 3, 4],
[ 3, 3],
[ 3, 2],
[ 3, 1],
[ 3, 0],
[ 2, 0],
[ 1, 0],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1]])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53243650%2fpython-replace-multiple-if-elif-statements-and-conditions-with-numpy-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
np.searchsorted is useful for this kind of logic:
def radio_index2xy_v(radio_index, n_x, n_y):
sgn = np.array([0, 1, 1, -1, -1, 0])
col = np.array([-1, 1, 0, 1, 0, -1])
coeffs = np.array([[-1, -1],
[0, -1],
[-n_x, n_x - 1],
[n_y - 1, 2*n_x + n_y - 2],
[2*n_x + 2*n_y - 3, 0],
[-1, -1]])
cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
idx = cusps.searchsorted(radio_index)
out = coeffs[idx]
out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
return out
Demo:
>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 1, 4],
[ 2, 4],
[ 3, 4],
[ 3, 3],
[ 3, 2],
[ 3, 1],
[ 3, 0],
[ 2, 0],
[ 1, 0],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1]])
add a comment |
np.searchsorted is useful for this kind of logic:
def radio_index2xy_v(radio_index, n_x, n_y):
sgn = np.array([0, 1, 1, -1, -1, 0])
col = np.array([-1, 1, 0, 1, 0, -1])
coeffs = np.array([[-1, -1],
[0, -1],
[-n_x, n_x - 1],
[n_y - 1, 2*n_x + n_y - 2],
[2*n_x + 2*n_y - 3, 0],
[-1, -1]])
cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
idx = cusps.searchsorted(radio_index)
out = coeffs[idx]
out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
return out
Demo:
>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 1, 4],
[ 2, 4],
[ 3, 4],
[ 3, 3],
[ 3, 2],
[ 3, 1],
[ 3, 0],
[ 2, 0],
[ 1, 0],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1]])
add a comment |
np.searchsorted is useful for this kind of logic:
def radio_index2xy_v(radio_index, n_x, n_y):
sgn = np.array([0, 1, 1, -1, -1, 0])
col = np.array([-1, 1, 0, 1, 0, -1])
coeffs = np.array([[-1, -1],
[0, -1],
[-n_x, n_x - 1],
[n_y - 1, 2*n_x + n_y - 2],
[2*n_x + 2*n_y - 3, 0],
[-1, -1]])
cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
idx = cusps.searchsorted(radio_index)
out = coeffs[idx]
out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
return out
Demo:
>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 1, 4],
[ 2, 4],
[ 3, 4],
[ 3, 3],
[ 3, 2],
[ 3, 1],
[ 3, 0],
[ 2, 0],
[ 1, 0],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1]])
np.searchsorted is useful for this kind of logic:
def radio_index2xy_v(radio_index, n_x, n_y):
sgn = np.array([0, 1, 1, -1, -1, 0])
col = np.array([-1, 1, 0, 1, 0, -1])
coeffs = np.array([[-1, -1],
[0, -1],
[-n_x, n_x - 1],
[n_y - 1, 2*n_x + n_y - 2],
[2*n_x + 2*n_y - 3, 0],
[-1, -1]])
cusps = np.cumsum([0, n_x, n_y-1, n_x-1, n_y-2])
idx = cusps.searchsorted(radio_index)
out = coeffs[idx]
out[np.arange(idx.size), col[idx]] += sgn[idx] * radio_index
return out
Demo:
>>> radio_index2xy_v(np.arange(20), 5, 4)
array([[-1, -1],
[ 0, 0],
[ 0, 1],
[ 0, 2],
[ 0, 3],
[ 0, 4],
[ 1, 4],
[ 2, 4],
[ 3, 4],
[ 3, 3],
[ 3, 2],
[ 3, 1],
[ 3, 0],
[ 2, 0],
[ 1, 0],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1],
[-1, -1]])
answered Nov 11 '18 at 8:17
Paul PanzerPaul Panzer
30k21240
30k21240
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53243650%2fpython-replace-multiple-if-elif-statements-and-conditions-with-numpy-function%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
3
Please tell us what the code is supposed to do, I don't think anybody is going to reverse engineer it.
– timgeb
Nov 10 '18 at 21:40
1
Also there's not a single
if/elifin this code.– timgeb
Nov 10 '18 at 21:41
This is used to find the x(vertical axis) and y(horizontal axis) from the given radio_index
– Yci Meow
Nov 11 '18 at 4:37
The if/elif method can be run.
– Yci Meow
Nov 11 '18 at 4:37