In matlab I can do the following:
>ind = [3,5,7,2,4,7,8,24]
ind = 3 5 7 2 4 7 8 24
>ind(1) ans = 3 ind(end) ans = 24 ind([1 end]) ans = 3 24
but I can't get the last line in python:
In [690]: ind = [3,5,7,2,4,7,8,24]
In [691]: ind[0] Out[691]: 3
In [692]: ind[-1:] Out[692]: [24]
In [693]: ??
How do I pull out multiple indices as in matlab?
Thanks, Evan 6 2069
Evan wrote in news:11*********************@j72g2000cwa.googlegro ups.com in
comp.lang.python:
In matlab I can do the following:
>>ind = [3,5,7,2,4,7,8,24]
ind = 3 5 7 2 4 7 8 24
>>ind(1) ans = 3 ind(end) ans = 24 ind([1 end]) ans = 3 24
but I can't get the last line in python:
In [690]: ind = [3,5,7,2,4,7,8,24]
In [691]: ind[0] Out[691]: 3
In [692]: ind[-1:] Out[692]: [24]
In [693]: ??
How do I pull out multiple indices as in matlab?
[ind[0], ind[-1]]
or if you need something that can be generalised:
[ind[i] for i in [0, -1]]
so if you have:
indexes_of_ind = [0, 2, -1, -2]
you can write:
[ind[i] for i in indexes_of_ind]
Rob.
-- http://www.victim-prime.dsl.pipex.com/
"Evan" <ev*******@gmail.comwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
In matlab I can do the following:
>>ind = [3,5,7,2,4,7,8,24]
ind = 3 5 7 2 4 7 8 24
>>ind(1) ans = 3 ind(end) ans = 24 ind([1 end]) ans = 3 24
but I can't get the last line in python:
In [690]: ind = [3,5,7,2,4,7,8,24]
In [691]: ind[0] Out[691]: 3
In [692]: ind[-1:] Out[692]: [24]
In [693]: ??
How do I pull out multiple indices as in matlab?
Thanks, Evan
Or use the third element of a slice, which defines a stepsize, and pick a
step that will go from the first to the last element:
>>lst = list("ABCDEFG") lst
['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>lst[0::len(lst)-1]
['A', 'G']
-- Paul
Evan wrote:
In matlab I can do the following:
ind = [3,5,7,2,4,7,8,24]
ind = 3 5 7 2 4 7 8 24
ind(1) ans = 3
ind(end) ans = 24
ind([1 end]) ans = 3 24
but I can't get the last line in python:
In [690]: ind = [3,5,7,2,4,7,8,24]
In [691]: ind[0] Out[691]: 3
In [692]: ind[-1:] Out[692]: [24]
In [693]: ??
How do I pull out multiple indices as in matlab?
If you want functionality similar to Matlab in Python, you should use
Numpy, which has the "take" function to do what you want.
Beliavsky wrote:
Evan wrote:
>In matlab I can do the following:
>>>ind = [3,5,7,2,4,7,8,24]
ind = 3 5 7 2 4 7 8 24
>>>ind(1) ans = 3 ind(end) ans = 24 ind([1 end]) ans = 3 24
but I can't get the last line in python:
In [690]: ind = [3,5,7,2,4,7,8,24] In [691]: ind[0] Out[691]: 3 In [692]: ind[-1:] Out[692]: [24] In [693]: ??
How do I pull out multiple indices as in matlab?
If you want functionality similar to Matlab in Python, you should use
Numpy, which has the "take" function to do what you want.
Actually, in numpy, we also have "fancy indexing" similar to Matlab's:
In [1]: from numpy import *
In [2]: ind = array([3,5,7,2,4,7,8,24])
In [3]: ind[[0, -1]]
Out[3]: array([ 3, 24])
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
It's quite straight forward, actually. What you need to know is that -1
is the index of the last element in a sequence, and that slicing
excludes the 'end' value in 'start:end'. So if you type arr[0:N], you
get the subsequence
[arr[0], arr[1], arr[2], ..., arr[N-1]]
When comparing with Matlab, Python slicing works like this:
arr(1:end) - arr[:] or arr[0:]
arr(1:end-1) -arr[:-1] or arr[0:-1]
arr(1:end-N) -arr[:-N] or arr[0:-N]
arr(end) -arr[-1]
arr(1) - arr[0]
arr(1:2:end) - arr[::2] or arr[0::2]
arr(1:2:end-1) -arr[:-1:2] or arr[0:-1:2]
Python slicing is not completely like Matlab, because it was adoped
from Haskell. It can do the same as Matlab's indexing, but the syntax
is different. If you think Matlab's indexing is more intuitive it is
just because you are more used to it.
Thanks for all the replies, it's much clearer now.
-Evan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mathias |
last post by:
Dear NG,
I currently ty to switch from matlab to python/scipy but have a lot of
trouble with images. What I need is a function for subsequently
displaying a number of 2D-matrices as an image.
I...
|
by: Mohammed Smadi |
last post by:
Hi;
Does anyone know if we can call matlab for a python or bash script while
feeding the matlab script some command line arguments? I have an
interactive matlab script which i want to automate...
|
by: James Curran |
last post by:
I'd like to make the following proposal for a new feature for the C#
language. I have no connection with the C# team at Microsoft. I'm posting
it here to gather input to refine it, in an "open...
|
by: Lars Christiansen |
last post by:
Hi!
I am a master student in (geo)physics at the University of Copenhagen
and part of a study group on C++ as a scientific programming language.
I, and the other students in the group, have...
|
by: rloef |
last post by:
I have a multi-million row table with three indexes in MySQL-5.0.15.
These indexes have the following number of distinct values:
date 415
block 100000
scan 45
If I'm doing a query...
|
by: Carl |
last post by:
I am desperately looking for a way to call Python from Matlab. I have become
used to Python's rich syntax and large number of libraries, and feel
ridiculously clumsy being stuck with Matlab's...
|
by: Sven Jerzembeck |
last post by:
Hello guys,
is there any possibiliy using Phython code in Matlab. I couldnt find
any helpful stuff. Its not graphical stuff I am doing just
calculations with arrays and strings.
Thanks for ...
|
by: Allen |
last post by:
Does anyone agree with me?
If you have used Matlab, welcome to discuss it.
|
by: Luna Moon |
last post by:
Dear all,
Can C++/STL/Boost do the vectorized calculation as those in Matlab?
For example, in the following code, what I really want to do is to
send in a vector of u's.
All other...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |