Skip to content

“Repmat” function in Numpy/Scipy?

January 17, 2010

I’m such a Matlab user. I just want to repeat an array or matrix X times, which I can do easily in Matlab using ‘repmat’. I’m sure this is super easy, and I’m just not seeing something obvious. Any tips? Is it something like concatenating copies of an array with itself? vstack?

Hey – I just found what I was looking for: I can do what I need to do here using the ’tile’ function in numpy. It works like this:

from numpy import *

x = array([1,2]) # create a simple array
bigx = tile(x,(2,1))

And the output is:
array([[1,2],
[1,2]])

Awesome! Now I can get rid of those pesky nested loops!

Also something interesting that I just discovered while messing with this in iPython: even if ‘x’ in this example is not created as an array (say it’s a list or a tuple) – it doesn’t matter – numpy still understands it, and then outputs an array object! Amazing. 🙂

Advertisements

Reverse axis direction in Pylab

January 14, 2010

I have been trying to remember how to do this FOREVER! I’m trying to plot a sound speed profile with depth along the y-axis and sound speed along the x-axis. But if I don’t reverse axes, the depth increases up, which isn’t really very intuitive.  In Matlab, it’s pretty easy.  But how to do it in Python/Pylab?  Here’s what the plot looked like before reversing the y-axis.

Here’s the code I used to reverse the axis:

from pylab import *

### SKIPPING SOME CODE HERE

# Plot sound speed profiles
plot(sspvector1,(depthvector1))
plot(sspvector2,(depthvector2),'r')

# Reverse the y-axis
ax = gca()
ax.set_ylim(ax.get_ylim()[::-1])

title('Sound speed profiles')
xlabel('Sound speed (m/s)')
ylabel('Depth (m)')
grid()
show()

And here’s the result:

Python – matrices or arrays?

January 14, 2010

Sometimes I think I have my biggest breakthroughs while biking home. Tonight, for instance, it dawned on me (on the stretch between the beach and Patterson) just how awful the Matlab version of my ray tracing code really is. And I realised that I could vastly improve its simplicity and efficiency by using matrices (Yikes! Why on earth did I implement all of those nested loops?). I pictured the steps and the math involved, the Matlab commands that I would need, and got really excited – until I realized that I don’t know how to do any of the same stuff in Python yet. First of all, I need to figure out how to work with arrays in Matlab. Thankfully, there’s a special page on Scipy.org Matlab users who are trying to migrate to Numpy/Scipy. Yes! And here it is: Numpy for Matlab Users. My first question was, “How do I make it do element-wise operations?” In Matlab, you need to modify the operator, for example using “.*” to multiply by elements, and just “*” to do matrix multiplication. Matlab thinks in linear algebra for 2d matrices. But, as I quickly learned, the default array in Numpy does element by element operations, unless you create a special matrix object. Awesome.

Installing Emacs on Windows

January 14, 2010

I tend to do everything I possibly can on Linux, but once in a while, I need to figure it out on Windows (*cringe). For example, today I am going to install Emacs on Windows. I quite literally have no clue how to do this, but a quick Google search turned up this page, which describes it in painful detail.

Next on my list: Databases

January 13, 2010

Wow, talk about getting sidetracked… I decided today that I want to learn about databases. I want to create a GUI (see previous two posts) and use it to write to and read from a database. I want to be able to search for keywords and numbers from various fields, etc. Somehow, it didn’t seem like it should be that hard. It’s sort of like a spreadsheet, right? I wanted to be able to make a work and survey log, with specific fields that I could fill in each day. Right now, I have something that sort of works, but it’s just an excel spreadsheet where I can filter the contents of the columns. But it’s getting really big and awkward, and not that pretty. I thought I could do better if I could just make a GUI specifically for that task. I did some Google searching, and, quite frankly, it scared me. Everything I found seemed to be aimed at people who already have a bit of a clue (um, definitely not me). So I contacted Kurt, who does all kinds of amazing stuff with databases. He suggested that I start with SQLite, and the Python SQLite module. These two links are various degrees of overwhelming, so I’m looking forward to some more guidance from Kurt! 🙂

Which Python GUI builder/designer?

January 12, 2010
tags: ,

Well, now I’m not sure which I should use… after my last post, Ian Witham pointed out to me that I do not *have* to use TKinter just because it’s the default. I can use other GUI designer software, for example, those built on wxPython. He suggested a couple, and said that he uses wxGlade himself. So now I don’t know which I should try first… is wxPython the way to go? Boa Constructor? PythonCard, Spe? Is there one that’s easier to learn than another (Ian says wxGlade is relatively easy to learn)…

Sometimes I get annoyed when I don’t know the basics like this, but I guess that’s just part of the process. Ah, my never-ending project to learn EVERYTHING (which at best will result in me knowing SOME things).

Tkinter and GUI building

January 12, 2010

I’m back to ray tracing again. And since I’m trying to re-create and expand on the Matlab version of this code, I am finding myself having to learn lots of little details – and big details. One of these things is GUI building. The standard GUI builder that comes with Python is TKinter. I’ve used this a bit before, but now I’m having to think about doing more complicated things with it. The Matlab version of my code used an interactive GUI. In Matlab, there’s a utility called GUIDE that gives you a GUI to build a GUI 🙂 You can put buttons, sliders, graphs, etc, into your GUI window, and then it will automatically generate a skeleton script that you can fill with commands telling Matlab what to do with it. So now I’m wondering if there’s something I can use with TKinter that does a similar thing. I’m sure there is, and I just need to find it…