2

I'm completely new to python and Numpy, and I'm trying to rewrite some python code in MATLAB. In the process I found this line which I don't understand a=np.array(eval(self.param[7]))[0:6].sum(); param is an structure containing 8 arrays something like this :

param[0]=100;  
param[1]=1;  
param[2]=1.5;                     
param[3]=(1,2,3,4);  
param[4]=(1,1,1,1);  
param[5]=10;  
param[6]=(1,0.,1,1,1,0.,0.,0.);  
param[7]=(2,0.,2,3,6,0.,0.,0.);

can someone tell me what does [0:6] and then.sum() do? My thought is a is a sum of all the 7 first element in the param[7], but it does not fit to the whole process.

1 Answer 1

2

The [0:6] will take the first 6 elements in param[7] producing the array

[2,0.,2,3,6,0.]

Then the .sum() method will sum the elements in that array.

Just a quick note on array slicing in python, the expression x[i:j:k] will take every kth element from the array x starting at the ith element, upto but not including the jth element.

If you miss any of them out then python will you assume that you mean the beginning of the array, the end of the array and 1 respectively. So the slice in your code could be slightly more succinctly written as [:6]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.