This question already has answers here:
MATLAB-style find() function in Python
(9 answers)
Is there a NumPy function to return the first index of something in an array?
(20 answers)
Replacement of matlab find function in python
(1 answer)
Converting find() in matlab to python
(3 answers)
Closed 1 year ago.
I have this iteration in a program in Matlab and want to translate it to Python, but my problem is in the parameters for 'n' and 'direction'.
for i=1:size(labels)
idx_V=[idx_V;find(y(idxUnls(trial,:))==labels(i),l/length(labels),'first')]
end
There isn't a one-to-one swap for MATLAB's find function in Python. Taking inspiration from another answer here, I would propose the following solution:
% MATLAB
inds = find(myarray == condition, n, 'first')
# Python
import numpy as np
inds = [ind for (ind, val) in np.ndenumerate(myarray == condition) if val]
inds = inds[0:n]
I'm sure there is probably some trickery to think about in terms of which dimension find operates over first, compared to ndenumerate. The Python expression could also be constructed as a generator.
If you want a similar implementation, you'll have to write it yourself in Python.
Related
This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 5 years ago.
After quite some effort, I'm still unable to find any clue about the meaning of the # character in python syntax, such as in the (provided to me) function
def PI(pi0,P=P,T=T):
# Function PI computes the state probability vectors
# of the Markov chain until time T
pi_ = array([pi0])
for i in range(T):
pi_ = vstack((pi_,pi_[-1] # P))
return pi_
(where pylab has been previously imported). At parsing, this character rises a SyntaxError message.Any clue welcome !
The name of the operator being used is the matrix multiplication operator. Here is the description of the operator from the documentation:
Return a # b.
New in version 3.5.
As you can see, it was first added in Python 3.5. Thus, if your getting a SyntaxError, you're likely using Python version 3.4 or lower.
This question already has answers here:
Python array multiply
(3 answers)
Closed 5 years ago.
I saw this questions a couple other places but all the fixes dont work for me code. If someone could help that would be great.
import random
cvalues=[]
for i in range(50):
cvalues.append(random.randrange(0,16))
float_cvalues=[float(i) for i in cvalues]
print(float_cvalues)
nvalues=[((.4*(float_cvalues)-.8))]
print(nvalues)
Multiplying a sequence in Python is interpreted as an attempt to create multiple sequences, see this post.
You can instead use another list comprehension:
nvalues=[.4*i-.8 for i in float_cvalues]
Or for instance switch to numpy arrays.
This question already has answers here:
Does Python support short-circuiting?
(3 answers)
Closed 5 years ago.
I have a question about a logical expression of the following sort:
for i in range (k): #k is large
if (a==b and test(c)==b): #test() takes some time to calculate
do something
Now I want to know, how the logical expression is processed. Are the two simple expressions calculated first and then combined via and? Or is a==b calculated, and in case it is False, test(c)==b neglected?
Thanks.
The a==b will be calculated first, and if it's true then the second expression will be evaluated. This is known as 'short-circuiting', see the docs.
This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.
This question already has answers here:
How to create a range of numbers with a given increment
(6 answers)
Closed 8 years ago.
Does anybody know whether Python can do the same thing as for i= 1:2:5 in Matlab? So i=1,3,5.
I know I can use other approaches to do this, but I want to know the equivalent form in Python.
try:
for i in xrange(1,6,2):
print i
This print:
1
3
5
Use xrange instead of range if you are using python 2.x because it is more efficient as it generates an iterable object, and not the whole list.
Use the range function:
for i in range(1, 6, 2):
print(i)