473,472 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Create linear spaced vector?

kjm
Hi Everyone,

I am trying to port some old MatLab code to python, and am stuck on
how to accomplish something.

I am trying to write a generalized function that will create a
linearly spaced vector, given the start and end point, and the number
of entries wanted.

In MatLab I have this function that I wrote:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function out = linearspace(x1,x2,n)
  3.  
  4. out = [x1+ (0:n-2)*(x2 - x1)/(floor(n)-1) x2];
  5.  
  6. return
  7.  
  8.  
  9.  

I have the numeric package, numarray installed, and I think it should
be accomplished easily, but I just can't seem to get the syntax
correct with python.

Any tips would be greatly appreciated.
Thanks
Jul 18 '05 #1
6 6511
On 2004-12-17, kjm <kj******@gmail.com> wrote:
I am trying to write a generalized function that will create a
linearly spaced vector, given the start and end point, and the number
of entries wanted.

from scipy import *
mgrid[0.0:10.0:5j]

array([ 0. , 2.5, 5. , 7.5, 10. ])

--
Grant Edwards grante Yow! Is this where people
at are HOT and NICE and they
visi.com give you TOAST for FREE??
Jul 18 '05 #2
On Fri, 2004-12-17 at 13:39, kjm wrote:
Hi Everyone,

I am trying to port some old MatLab code to python, and am stuck on
how to accomplish something.

I am trying to write a generalized function that will create a
linearly spaced vector, given the start and end point, and the number
of entries wanted.

In MatLab I have this function that I wrote:

Expand|Select|Wrap|Line Numbers
  1.  function out = linearspace(x1,x2,n)
  2.  out = [x1+ (0:n-2)*(x2 - x1)/(floor(n)-1) x2];
  3.  return
  4.  


I have the numeric package, numarray installed, and I think it should
be accomplished easily, but I just can't seem to get the syntax
correct with python.

Any tips would be greatly appreciated.
Thanks


Is this want you want?

#!/usr/bin/python

def linear_space( start, end, count ):

""" Returns a vector containing count evently spaced intervals
(count + 1 evenly spaced points) """

delta = (end-start) / float(count)
return [start,] + \
map( lambda x:delta*x + start, range( 1, count ) ) + [end, ]

if __name__ == "__main__":
print linear_space( 1.0, 2.0, 10 )

Running it gives you:
[1.0, 1.1000000000000001, 1.2, 1.3, 1.3999999999999999, 1.5,
1.6000000000000001, 1.7000000000000002, 1.8, 1.8999999999999999, 2.0]

Adam DePrince
Jul 18 '05 #3
>>>>> "kjm" == kjm <kj******@gmail.com> writes:

kjm> Hi Everyone, I am trying to port some old MatLab code to
kjm> python, and am stuck on how to accomplish something.

kjm> I am trying to write a generalized function that will create
kjm> a linearly spaced vector, given the start and end point, and
kjm> the number of entries wanted.

kjm> In MatLab I have this function that I wrote:

kjm> [code]

kjm> function out = linearspace(x1,x2,n)

in matlab the builtin function to accomplish this is "linspace"

The python package matplotlib defines a host of matlab compatible
functions, including linspace

def linspace(xmin, xmax, N):
if N==1: return xmax
dx = (xmax-xmin)/(N-1)
return xmin + dx*arange(N)
Note that matplotlib extends the Numeric/numarray core of matlab
compatible functions (defined in MLab) to include plotting functions

http://matplotlib.sourceforge.net

A listing of matlab compatible functions is provided at
http://matplotlib.sourceforge.net/matplotlib.pylab.html
JDH

Jul 18 '05 #4
Thanks for the code snippets guys. Exactly what I needed to get going.

I knew I could get the solution from matplotlib, but getting it
installed using Fink (OS X) has been giving me a headache, so I thought
I could just write my own function for now to get a small piece of code
written....

The help is greatly appreciated.

kjm

John Hunter wrote:
>> "kjm" == kjm <kj******@gmail.com> writes:


kjm> Hi Everyone, I am trying to port some old MatLab code to
kjm> python, and am stuck on how to accomplish something.

kjm> I am trying to write a generalized function that will create
kjm> a linearly spaced vector, given the start and end point, and
kjm> the number of entries wanted.

kjm> In MatLab I have this function that I wrote:

kjm> [code]

kjm> function out = linearspace(x1,x2,n)

in matlab the builtin function to accomplish this is "linspace"

The python package matplotlib defines a host of matlab compatible
functions, including linspace

def linspace(xmin, xmax, N):
if N==1: return xmax
dx = (xmax-xmin)/(N-1)
return xmin + dx*arange(N)
Note that matplotlib extends the Numeric/numarray core of matlab
compatible functions (defined in MLab) to include plotting functions

http://matplotlib.sourceforge.net

A listing of matlab compatible functions is provided at
http://matplotlib.sourceforge.net/matplotlib.pylab.html
JDH


Jul 18 '05 #5
>>>>> "kjmacken" == kjmacken <kj******@gmail.com> writes:

kjmacken> Thanks for the code snippets guys. Exactly what I
kjmacken> needed to get going. I knew I could get the solution
kjmacken> from matplotlib, but getting it installed using Fink (OS
kjmacken> X) has been giving me a headache, so I thought I could
kjmacken> just write my own function for now to get a small piece
kjmacken> of code written....

Yes, matplotlib fink installs have frustrated many an OSX user. Note
that the matplotlib.mlab package (where linspace and others functions
reside) do not require any extension code and can be reused anywhere
you like as python code by copying and pasting, etc.

Also, Robert Kern is in the process of building an "enthon" package
for OSX that has most of the utilities for scientific computing
including matplotlib built-in. Batteries included on steroids,
basically.

http://www.scipy.org/wikis/featurerequests/MacEnthon

So keep your eyes on that site for release information.

JDH
Jul 18 '05 #6
John,

Thanks for the heads up RE: scipy, I will keep my eyes on the
developments.

Also, thanks for the info:

that the matplotlib.mlab package (where linspace and others functions
reside) do not require any extension code and can be reused anywhere
I was able to get these modules into my site-packages directory, and
make use of what is there.

Cheers,

kjm

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rakesh Sinha | last post by:
This is about the vector template defined in standard C++ . Suppose I want to create a *huge* vector , as follows. void f1() { vector < int > data(1024); //may be not very huge, but for...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
2
by: ernesto | last post by:
Hi: I want to create my own vector class; I want to provide methods like: class Vector { public: void add(const Object* aVal); void remove(const Object* aVal); };
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
5
by: Jazi | last post by:
I am newbie to c++ and I would like to build a vector class that returns a vector of objects. I have done some thing like this: student class that contains some of student's properties such as...
11
by: Holgerson | last post by:
Hi everybody, what I try to do is to implement an operator* into NRVec in such a way, that I can perform an operation like number*vector rather than vector*number which is easy. Any ideas?...
7
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>;...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.