Vincent Cheung

Vincent Cheung's Blog


« Newer Posts Home Older Posts »

Monday, July 19, 2004

More Matlab Tricks

Neither Anitha nor Brendan knew about this (I was coding on the fly during our last meeting).

To kill a row in a matrix in Matlab do the following:
>> a = magic(3)

a =

8 1 6
3 5 7
4 9 2

>> a(2, :) = []

a =

8 1 6
4 9 2

Works just as well for columns. You can also remove individual elements, but if you do it in a matrix, you'll end up with a vector.

I titled this "More Matlab Tricks" cuz I had two tricks to record, but forgot the second one.....doh....

Powerbook

Btw, I did get a Powerbook (12" G4 1.33 GHz with Superdrive). The education price was about $2150. Brendan's gonna reimburse me for it.

Nice machine. Wireless works nicely and got MS Office 2004, FireFox, Thunderbird, Azureus, and LaTeX running. A lot of the software I'm using is the same as on Windows. Even have Microsoft Remote Desktop Client for Mac OS X to remotely access computers :)

The only thing I'm missing right now is Matlab. I'm waiting for Matlab 7 as it has just come out and I might as well put the newest on it. Plus, 6.5 doesn't support 10.3, you need 6.5.1 (sp1), which I don't have and I couldn't find it. I now have the Matlab 7 install files (on the network at school), but don't have a license yet...

Not sure if the Windows and Mac / Linux / Unix license are the same, otherwise, I could just use the one I use at home...

Indexing in a circularly wrapped matrix in Matlab

Neat trick in Matlab to index a matrix in a circularly wrapped manner (eg. with image / video epitomes). Also generally useful for indexing into matrices.

The key idea is to use vectors to index into the matrix. This is made easier with cells (especially when dealing with multidimensional arrays).

Take this example:

>> a = pascal(6)

a =

1 1 1 1 1 1
1 2 3 4 5 6
1 3 6 10 15 21
1 4 10 20 35 56
1 5 15 35 70 126
1 6 21 56 126 252

>> b = magic(4)

b =

16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

>> subs = {mod(4:7, 6)+1, mod(4:7, 6)+1}; subs{:}

ans =

5 6 1 2


ans =

5 6 1 2

>> a(subs{:})

ans =

70 126 1 5
126 252 1 6
1 1 1 1
5 6 1 2

>> a(subs{:}) = b

a =

6 12 1 1 9 7
15 1 3 4 4 14
1 3 6 10 15 21
1 4 10 20 35 56
3 13 15 35 16 2
10 8 21 56 5 11


Executive summary: took a 4x4 circularly wrapped patch in "a" and replaced with "b" and the indexing was made cleaner with the use of a cell


You could also just simply do the following:
a([5 6 1 2], [5 6 1 2]) = b

but using the cell gives you more flexibility when using this in "real" situations where you probably have to do this a number of times.
« Newer Posts Home Older Posts »