Basic Matlab to python question
14 views (last 30 days)
Show older comments
Commented: H on 27 Jan 2023
I am a 'native Matlaber' attempting to run some things on python. I was wondering if you could help me with a seemingly simple question.
how would I convert the Matlab line
I am aware this is not directly a Matlab question, but any help would be apreciated.
Al Danial on 27 Jan 2023
The Python equivalent is much more verbose than matlab's [6:10,1:4]:
In : import numpy as np
In : np.hstack((np.arange(6,11), np.arange(1,5)))
Out: array([ 6, 7, 8, 9, 10, 1, 2, 3, 4])
NumPy's hstack() function does a horizontal concatenation of arrays. The other quirk is that one must specify arange(a, b+1) to get the equivalent of matlab's a:b.
More Answers (1)
Askic V on 25 Jan 2023
This is what you probably want:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
arr2 = np.arange(0,10,2)
This leads to the following output:
[1 2 3 4 5]
[0 2 4 6 8]
[2 4 6]
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!