Basic Matlab to python question

조회 수: 80 (최근 30일)
H
H 2023년 1월 25일
댓글: H 2023년 1월 27일
Hi all,
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
array([6:10,1:4])
into python?
I am aware this is not directly a Matlab question, but any help would be apreciated.
Thank you!
  댓글 수: 2
Pavel
Pavel 2023년 1월 25일
What exactly do you mean?
Your code looks like a Python code to me, do you want this now as Matlab code?
If so I recommend this tutorial series:
https://www.mathworks.com/help/matlab/learn_matlab/matrices-and-arrays.html
H
H 2023년 1월 25일
편집: H 2023년 1월 25일
Apolagies, I could be clearer, I mean the Matlab code where I select some of the end values to be at the beggining, and select some of the beggining values to go at the end in an array.
How would I do this is python?

댓글을 달려면 로그인하십시오.

채택된 답변

Al Danial
Al Danial 2023년 1월 27일
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.

추가 답변 (1개)

Askic V
Askic V 2023년 1월 25일
This is what you probably want:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
arr2 = np.arange(0,10,2)
print(arr)
print(arr2)
print(arr2[1:4])
This leads to the following output:
[1 2 3 4 5]
[0 2 4 6 8]
[2 4 6]
  댓글 수: 1
H
H 2023년 1월 25일
This is a good step but not quite what I am looking for.
My end goal is to get an array like-
[6 7 8 9 10 1 2 3 4 5]
(example for simplsty, im actually looking to rearange a bigger array, only selcting the last few and first few values e.g. [ 111 112 113 1 2 3]).
Thanks for the help though.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by