Showing posts with label math. Show all posts
Showing posts with label math. 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, March 07, 2011

MATLAB script to prove to you that the perfect squares are the switches that are toggled

Evidently, this problem exists in both "switch" and "locker" forms. Assume that there are 100 switches are initially off and numbered from 1 to 100.
  • First, you toggle multiples of 1 (i.e., every switch gets turned on),
  • and then you toggle multiples of 2 (i.e., every even-numbered switch gets turned off),
  • and then you toggle multiples of 3 (i.e., switches 3, 6, 9, 12, ... are toggled),
  • ...
  • and then you toggle multiples of 99 (i.e., switch 99 is toggled),
  • and then you toggle multiples of 100 (i.e., switch 100 is toggled).
After this process, which switches are "on"?

Examining the problem, you notice that switches are toggled the same number of times as they have unique factors. For example, switch 6 is toggled 4 times, which corresponds to its unique factors, 1, 2, 3, and 6. However, switch 4 is toggled only 3 times, which corresponds to its unique factors, 1, 2, and 4 (i.e., even though 4 = 2 × 2, the factor 2 is only represented in the list once because it is duplicated). Moreover, it is only the perfect squares (1, 4, 9, 16, 25, 36, 49, 64, 81, 100) that will have repeated factors (i.e., their whole square roots), and so it is only those switches that will be flipped and odd number of times. Furthermore, it is only those switches that will be "on" after this game.

To verify this to yourself, use a MATLAB script:
% All switches initially off
switches = zeros(1,100);

% Toggle multiples of the current switch, including the current switch
for i = 1:100
% These are the switches to toggle
ivec = i:i:100;

% xor'ing them with 1's toggles them
switches(ivec) = bitxor(switches(ivec), 1);
end

% Show the indexes of the switches that are "on" after this process
% (expect perfect squares: 1 4 9 16 25 36 49 64 81 100)
find( switches )
Running the script results in:
ans =

1     4     9    16    25    36    49    64    81   100
which is what we expected.

Of course, you could also keep track of how many times you made a flip. XOR can help with this too.
% All switches initially off
switches = zeros(1,100);
new_switches = zeros(1,100);
num_flips = zeros(1,100);

% Toggle multiples of the current switch, including the current switch
% (gratuitous use of bitxor as a demo)
for i = 1:100
% These are the switches to toggle
ivec = i:i:100;

% xor'ing them with 1's toggles them
new_switches(ivec) = bitxor(switches(ivec), 1);

% count the flips
% ( num_flips(ivec) = num_flips(ivec)+1  works too)
num_flips = num_flips + bitxor( new_switches, switches );

% commit new_switches to switches to maintain invariant
% (can omit new_switches by not using second bitxor above)
switches = new_switches;
end

% Show the indexes of the switches that are "on" after this process
% (expect perfect squares: 1 4 9 16 25 36 49 64 81 100)
find( switches )
Then, from the console, we can issue commands like:
num_flips( find(switches) )     % shows number of flips for switches ending "on"
num_flips( find(switches==0) )  % shows number of flips for switches ending "off"
find( mod(num_flips,2)==1 )     % verifies odd-count flips have perfect-square indexes
So that's fun.

Saturday, July 07, 2007

Aurora: Ribbit all grown up

I just found out that Ribbit, the program that gave LaTeX support to Microsoft® Word (on Microsoft® Windows), has changed its name to Aurora and has lots more features. From its web page:
  • No more mouse-hunting for symbols—enter great-looking math quickly and efficiently using the standard language of scientific typesetting.
  • Express any scientific concept from mathematics, computer science, chemistry, engineering, and many other areas.
  • Full integration with Microsoft® Word, PowerPoint®, Visio®, and Excel®.
  • Built-in LaTeX document converter.
  • Advanced support for equation numbering.
  • Works with any Windows application that has an “Insert Object...” function or lets you paste images.
In short, Aurora brings to Windows what a countless many applications (e.g., LaTeX Equation Editor) bring to OS X.

Auroa gives a LaTeX support to Windows applications.

There's a 30-day free trial. After that, Aurora is $45 for regular users and $35 for academic users. If you don't have MiKTeX (or some alternative) installed, it can install a free micro version for you.

Other notable examples of Windows applications with LaTeX integration include AbiWord, OpenOffice (see OOoLatex), and TeXmacs.

My LaTeX-using colleagues would want me to reminder the reader that if you know enough LaTeX to be entering equations using it, you probably should just be using LaTeX directly. Why fake it? Despite Aurora's claim that it is "even better than the real thing," it's a distant second at best. That being said, it is SO much better than the Microsoft® Equation Editor, which should absoultely never be used.