How to write A[1:2:9](Matlab) in Python

This is the place for queries that don't fit in any of the other categories.

How to write A[1:2:9](Matlab) in Python

Postby anastasi.fr » Sat Oct 19, 2013 2:22 pm

Hi....
i have in matlab this matrix A=[1 2 3;4 5 6;7 8 9] if i type A[1:2:9] the answer is [1 3 5 7 9].....
i read some tutorial on internet, i learnt how to write A(i,:) or A(:,j) in python but i don't know how to write A[1:2:9]...
could you help me??
anastasi.fr
 
Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

Re: How to write A[1:2:9](Matlab) in Python

Postby anastasi.fr » Sun Oct 20, 2013 12:38 pm

i find the solution:
if i have an array u0=array([[1],[2],[3],[4],[5],[6],[7],[8],[9],[0]])
the command-------> u0.flatten()[from:to:step] like this u0.flatten()[0:6:2] give this answer, 1 3 5 7.....
anastasi.fr
 
Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

Re: How to write A[1:2:9](Matlab) in Python

Postby Kebap » Sun Oct 20, 2013 1:03 pm

Like this?
Code: Select all
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print x[::2]
[1, 3, 5, 7, 9]
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: How to write A[1:2:9](Matlab) in Python

Postby anastasi.fr » Sun Oct 20, 2013 1:41 pm

no....with your code you start from 1-position and step by 2 you arrive at the end of the array....in the other way you could define the stop point inside matrix...
anastasi.fr
 
Posts: 22
Joined: Fri Oct 18, 2013 10:17 am

Re: How to write A[1:2:9](Matlab) in Python

Postby Kebap » Sun Oct 20, 2013 2:23 pm

You can define start and end as well:
Code: Select all
>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print x[2:6:2]
[3, 5]
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests