TinyURL widget - shorten your URL's for free!

Enter a long URL to make tiny:

Monday, September 15, 2014

Removing Dimensions from Matrices in Octave or MATLAB - squeeze()

For many math applications you want to run through many variables all at once and gather data. Of course, having several variables that will lead to n-dimensional matrices.

For example I was converting from a spherical coordinate system to a Cartesian and experimenting with what the x,y,z coordinates would be for two changing angles.  This lead to a rank (3 by n by m)  3-dimensional matrix where first dimension is the xyz, the second is angle one and the last is angle two.  To get the matrix for any single variable, do the following: extract a submatrix and then use squeeze to get a single 3 by n or 3 by m matrix.

Example:

Let A = [3*n*m]  say=
size(A) = 3 32 32

a = A(:,1,m)   # takes single dimension matrix from 3*m*n using column one of second dimension

size(a) = 3 1 32

a = squeeze(a) 

size(a) = 3 32

squeeze() looks for the singleton dimension and then does the tedious  matrix copy and paste operations for you.


Of course this gives you one of many, then duplicate for the remainder.

No comments:

Post a Comment