# Doug's MATLAB Video Tutorials

## MATLAB Basics video: Row and Column indexing

This is one in a series of videos covering MATLAB basics. It is meant for the new MATLAB user. This video covers how to use row and column notation to be able to pull a subset of data from a larger matrix. This is a basic skill that is required for anyone that is going to use MATLAB. This covers the form mat(row,col) with some of the fancier techniques like using “:” or “end”.
Find the files here. Other videos have been gathered here: http://blogs.mathworks.com/pick/category/video/ Other MATLAB Basics posts have been gathered here: http://blogs.mathworks.com/pick/category/matlab-basics/

### 3 Responses to “MATLAB Basics video: Row and Column indexing”

1. Tim Davis replied on :

One mistake that both new and experienced MATLAB users make is to abuse A(i,j)=x and x=A(i,j) when A is a large sparse matrix. Both work fine, but they can be very slow in some cases. x=A(1,:) for example is much slower than x=A(:,1). There are often better ways to write your code that avoids these slow cases. For example, if you find yourself needing to do x=A(i,:) a lot, for a matrix A that doesn’t change, then transpose A once and do x=A(:,i) instead (your code could be 100x faster as a result). The sparse matrix data structure in MATLAB is best suited to whole-matrix operations, rather than row and column indexing.

None of this comment applies to full matrices, just sparse. You don’t get sparse matrices unless you ask for them, so if you’re a MATLAB newcomer who also has no need for sparse matrices, then please ignore my comment.

2. Doug replied on :

Tim gives good advice here. It should be noted that the kinds of optimizations he is talking about shave small fractions of a second off of a single operation in typical situations.

These fractions add up though when you are in a loop that gets called many times. The profiler is very good at helping to identify these places. See my early videos for use of the profiler.

Doug

3. Tim Davis replied on :

That’s right; I should have made that more clear. If you’re dealing with modest-sized problems, you won’t see this effect. The danger is when you want to scale up to solve large problems, and you wrongly conclude that MATLAB can’t handle large problems.

A grad student at Stanford (Sep Kamvar) asked me why his pagerank-style computations in MATLAB were so exceedingly slow when dealing with a matrix of dimension about 680,000. He showed me the code, and I saw x=A(i,:) in a loop. Aha! … and when it was fixed his run time was cut from maybe an hour to under a minute. For his matrix, x=A(i,:) takes 0.16 seconds, but x=A(:,i) takes 0.0015 seconds. If you’re curious, you can try it with his matrix at http://www.cise.ufl.edu/research/sparse/matrices/Kamvar/ .

The profiler is a great tool for finding hot spots like this. It won’t help you to figure out why x=A(i,:) is slow and x=A(:,i) is 100′s of times faster, though. It may seem a little mysterious unless you look into how sparse matrices are stored in MATLAB.

Other “gotcha’s” like this related to matrix indexing include, for full matrices and sparse alike:

x = [ ] ;
for i = 1:100000
x (i) = i ;
end

for which mlint is a great tool for suggesting a much faster alternative. The code above takes 50 seconds; using mlint’s suggestion and changing the first line to x=zeros(1,100000); cuts the time to 0.12 seconds (yes, of course I’m just doing x=1:100000; this is just an example).

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 7 + 10 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

These postings are the author's and don't necessarily represent the opinions of MathWorks.