Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, August 23, 2011

The maximum number of matrix dimensions in MATLAB

[ Background: I was asked what the maximum number of matrix dimensions was in MATLAB today. I responded as follows. ]

You are only limited by the amount of memory available and the maximum number of ELEMENTS (as opposed to dimensions) in a matrix. The actual number of dimensions is just a detail about how the memory is indexed. You can reshape any existing matrix to any number of dimensions (I'll give details below). You can only create a new matrix if it abides by the memory and element limits that vary by computer.

To find out the maximum number of elements for a matrix on your computer, use the MATLAB command "computer" (do "help computer" for details). For example:
[~,maxsize,~]=computer
tells me that I can have 2.8147e+14 elements in matrices on my computer. So I better be sure that:
(number of rows)
   × (number of columns)
   × (number of cubes)
   × (number of 4-th dimensional thinggies)
   × (...)
is less than that number.

To find out about memory limits on your system see, the command "memory" ("help memory" or "doc memory"). Unfortunately, memory may not be available on your system. Alternatively, you can see:

http://www.mathworks.com/support/tech-notes/1100/1110.html

for information about memory limits in MATLAB. For information about the maximum number of elements (and the command "computer" that I discussed above), see (UPDATE: MATLAB has moved this page, and this link doesn't land in the right spot anymore):

http://www.mathworks.com/support/tech-notes/1200/1207.html#15

Regarding dimensions, you can use the command "reshape" to re-index any existing matrix. For example, if I start with the column vector:
A=ones(100,1)
I can turn it into a row vector:
newA = reshape(A, 1, 100)
or a matrix of any number of dimensions so long as the number of elements is still 100.
newA = reshape( A, 2, 2, 25 )
newA = reshape( A, 1, 1, 1, 1, 1, 1, 1, 1, 1, 100, 1 )
newA = reshape( A, 1, 1, 1, 2, 1, 50, 1, 1, 1, 1, 1, 1, 1, 1 )
% etc.
Now, I'm assuming you're using regular MATLAB matrices. Alternatively, you can use sparse matrices so long as you limit yourself to functions that work with sparse matrices:
help sparfun
A sparse matrix stores an index with every element. That lets it "skip over" the 0 elements of the matrix. Consequently, you can store VERY large matrices with an abstract number of elements far larger than anything you can work with in MATLAB... however, most of those abstract elements will be 0.

Monday, June 20, 2011

Spec# is a terrible name. C-clef would have been better.

Microsoft Research's RiSE has another pre-print out on Spec#. The Spec# specification language is old news by now, and so it's unfortunate that the name "Spec#" has not been changed because it means it probably is going to stick. Unlike the name "C#", "Spec#" is terribly unimaginative. It's like naming your first child "Hermione" and then naming your second child "Two".

Wouldn't it have made more sense to continue the musical analogy? For example, a C-clef is a conventional symbol from music theory that is used to specify the desired meaning of the lines that follow. Thus, it makes a lot of sense to use it as a name for specification language for C#, right?

Instead, we get Spec# (i.e., "specsharp"), which actually seems quite dull...

Wednesday, February 16, 2011

More Quick Reference Cards

Remember that time when I was quick reference card happy [1, 2, 3, 4, 5]? Today, I accidentally found another good quickref card blog post by someone else (Refcards by Michael Goerz; see the original post for the source code and other versions of these cards):He also includes a few quick reference cards not written by him (Subversion (SVN), GDB, GNU Emacs, MySQL).

Tuesday, January 04, 2011

Drawing a circle in MATLAB (curved rectangle instead of approximating with filled polygon)

Today, someone I know googled for some help drawing a filled circle in MATLAB and asked me what I thought of the code that was found. The code found, which seems to be the conventional way to draw circles in MATLAB according to Internet posters, generates a polygonal approximation of a circle and then fills it. It goes something like this (where you can use linspace(0,2*pi,N+1) and daspect([1,1,1]) (or axis square) instead of two of the commands used here):
xc = 0;
yc = 0;
r = 2;
N = 256;
theta = (0:N)*2*pi/N;
x = r*cos(theta) + xc;
y = r*sin(theta) + yc;
fill(x, y, 'k');
axis equal;
where (xc,yc) is the coordinate of the center of the circle, r is the radius of the circle, and (N-1) is the number of sides of the polygon that is inscribed inside the desired circle (i.e., the polygon's corners intersect the circle). The circle is filled with the color black, which corresponds to the 'k' parameter of the fill command. To quickly get rid of the circle without clearing the figure, change the fill command so its figure handle gets assigned to a variable (e.g., h = fill(x,y,'k');) and you can delete that figure handle later (e.g., delete(h);). I notice that some people precede the fill command with a plot(x,y); command, but that is not necessary; the fill command will construct the closed finite polygon and put a (black) border around it by default, and so there is no need to draw the border ahead of time with plot.

[ Of course, there are methods very similar to the above that use a rotating complex phasor (i.e., a complex exponential from r*exp(i*theta)) and then let plot generate the projected Cartesian plane coordinates from the complex plane. These methods are equivalent but depend on MATLAB to do the trigonometry behind the scenes, which may or may not be faster... ]

What surprises me is that there is a perfectly nice built-in MATLAB command that will draw rectangles, rounded rectangles, circles, and ellipses very quickly essentially without having to approximate them with finite polygons. It is the rectangle command. Some of the people who posted code like the above example apparently know about the rectangle command (because they use it to verify that their circular approximation is sufficiently circular); however, I guess it's not ideal for them because it references the figure to a corner as opposed to its center. Nevertheless, a lot of people who might not be as picky may not know about the rectangle command, and so here's an example similar to the above.
w = 4;
xc = 0;
yc = 0;
rectangle('Curvature',[1 1], ...
          'Position',[xc-w/2 yc-w/2 w w], ...
          'FaceColor','k');
axis equal;
The 'Curvature' property tells MATLAB that the lines connecting the corners of the rectangle should have no straight portions. By decreasing each of the 1's to 0 in the curvature property, the circle looks more like a rounded rectangle with flat sides and rounded corners. The 'Position' property places the bottom-left corner of the rectangle in its first two elements and then sets the width and height of the "rectangle". So here, a circle centered at (xc, yc) is drawn with a radius of w/2. The circle is filled with 'FaceColor'. Again, axis equal; could be replaced with daspect([1,1,1]); or axis square;. Additionally, rectangle can be assigned to a variable (e.g., h = rectangle(...);) so that the rectangle can be easily removed later (e.g., delete(h);) without having to clear the figure. So this example draws a circle, but by playing with the rectangle parameters, it is easy to draw rectangles, rounded rectangles, and ellipses as well.

It is possible that the former method might be faster, but I can't imagine it would be much faster. The latter method seems more elegant and intuitive to me, and I have to imagine that's what the people at Mathworks would advise you to use if you called them up. Having said that, I have not called them up, and so you decide for yourself.