Daniel Crane
Daniel Crane

Reputation: 257

Efficiently extracting column of a matrix

I currently have a piece of code that I'm trying to optimise, and the bottleneck seems to be extracting a given column from a fairly large matrix.

In particular, my code spends 50% of its time doing Wi=W(:,minColIdx). I've also tried linear indexing, but there was no change.

I was wondering if anyone knows why this is the case, and if anyone has any tips that could help me optimise this part of my code.

Thanks!

EDIT: Here is my code: http://pastebin.com/TnTy6a8D It's really poorly optimised right now, I was just playing around a bit with gpuArray on my new GPU. Lines 44 and 53, where I try to extract columns from W, are where the code bottlenecks.

Upvotes: 1

Views: 96

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Can the speed of the operation be improved?

Of course

Is it worth it to optimize the indexing code?

Probably not


Matlab is REALLY good at basic matrix operations (if doing it in C++ is 10% faster I would really be surprised). You can forget about finding a better way to index a matrix, if you really want a noticable performance increase improving your hardware is probably your best bet.

That being said, it is of course always worth thinking about whether you really need to do the heavy calculation that you are attempting, or whether you can think of a smarter algorithm.

Upvotes: 1

pablo honee
pablo honee

Reputation: 105

I'll take at face value your statement about the bottleneck being extracting the column from a matrix since you don't provide enough detail for me to speculate otherwise, though I find this somewhat surprising.

If you have access to the matlab compiler, I suggest you try compiling your bottleneck function. Try:

help mcc

From within that help you will see that a typical use is:

Make a stand-alone C executable for myfun.m:

mcc -m myfun

You could also try writing a c function to get your column and compile with mex: http://www.mathworks.com/help/matlab/ref/mex.html

Upvotes: 0

Related Questions