2.8.3.2 Matrix Object Data Manipulation

In addition to the matrix command, Origin provides X-Functions for performing specific operations on matrix object data. In this section we present examples of X-Functions that available used to work with matrix object data.

Set Values in Matrix Object

Matrix cell values can be set either using the matrix -v command or the msetvalue X-Function. The matrix -v command works only on an active matrix object, whereas the X-Function can set values in any matrixsheet.

This example shows how to set matrix values and then turn on display of image thumbnails in the matrix window.

// Create a matrixbook
newbook mat:=1; 
int nmats = 10; 
range msheet=1!;
// Set the number of matrix objects
msheet.Nmats = nmats;
// Set value to the first matrix object 
matrix -v x+y;
range mm=1; mm.label$="x+y"; 
double ff=0; 
// Loop over other objects
loop(i, 2, nmats-1) { 
	msheet.active = i; 
	ff = (i-1)/(nmats-2);
	// Set values
	matrix -v (5/ff)*sin(x) + ff*20*cos(y); 
	// Set LongName 
	range aa=$(i); 
	aa.label$="$(5/ff,*3)*sin(x) + $(ff*20)*cos(y)"; 
}
// Fill last one with random values
msheet.active = nmats; 
matrix -v rnd(); 
range mm=$(nmats); mm.label$="random";
// Display thumbnail images in window
matrix -it;

Copy Matrix Data

The mcopy X-Function is used to copy matrix data.

// Copy data from mbook1 into another matrix, mbook2.
mcopy im:=mbook1 om:=mbook2; // This command auto-redimensions the target

Conversion between Matrix Object and Vector

Two X-Functions, m2v and v2m, are available for converting matrix data into a vector, and vector data into a matrix, respectively. Origin uses row-major ordering for storing a matrix, but both functions allow for column-major ordering to be specified as well.

// Copy the whole matrix, column by column, into a worksheet column
m2v method:=m2v direction:=col;
// Copy data from col(1) into specified matrix object
v2m ix:=col(1) method:=v2row om:=[Mbook1]1!1;

Conversion between Numeric Data and Image Data

In Origin, matrices can contain image data (i.e., RGB) or numeric data (i.e., integer). The following functions are available to convert between the two formats.

// Convert a grayscale image to a numeric data matrix
img2m img:=mat(1) om:=mat(2) type:=byte; 
// Convert a numeric matrix to a grayscale image
m2img bits:=16;

Manipulate Matrix Object with Complex Values

X-Functions for manipulating a matrix with complex values include map2c, mc2ap, mri2c, and mc2ri. These X-Functions can merge two matrices (amplitude and phase, or real and imaginary) into one complex matrix, or split a complex matrix into amplitude/phase or real/imaginary components.

// Combine Amplitude and Phase into Complex
map2c am:=mat(1) pm:=mat(2) cm:=mat(3); 
// Combine Real and imaginary in different matrices to complex in new matrix
mri2c rm:=[MBook1]MSheet1!mat(1) im:=[MBook2]MSheet1!mat(1) cm:=<new>;
// Convert complex numbers to two new matrix with amplitude and phase respectively
mc2ap cm:=mat(1) am:=<new> pm:=<new>; 
// Convert complex numbers to two matrix objects with real part and imaginary part
mc2ri cm:=[MBook1]MSheet1!Complex rm:=[Split]Real im:=[Split]Imaginary;

Transform Matrix Object Data

Use the following X-Functions to physically alter the dimensions or contents of a matrix. In the transformations below, except the flipping matrix object, others may change the dimensions of its matrixsheet, which will make the change on other matrix objects in this matrixsheet.

Crop or extract from Data or Image Matrix

When a matrix contains an image in a matrix, the X-Function mcrop can be used to extract or crop to a rectangular region of the matrix.

// Crop an image matrix to 50 by 25 beginning from 10 pixels 
// from the left and 20 pixels from the top.
mcrop x:=10 y:=20 w:=50 h:=25 im:=<active> om:=<input>; // <input> will crop
// Extract the central part of an image matrix to a new image matrix
// Matrix window must be active
matrix -pg DIM px py;
dx = nint(px/3);
dy = nint(py/3);
mcrop x:=dx y:=dy h:=dy w:=dx om:=<new>; // <new> will extract

Expand Data Matrix

The X-Function mexpand can expand a data matrix using specified column and row factors. Biquadratic interpolation is used to calculate the values for the new cells.

// Expand the active matrix with both factor of 2
mexpand cols:=2 rows:=2;

Flip Data or Image Matrix

The X-Function mflip can flip a matrix horizontally or vertically to produce its mirror matrix.

// Flip a matrix vertically
mflip flip:=vertical;

// Can also use the "matrix" command
matrix -c h;  // horizontally
matrix -c v;  // vertically

Rotate Data or Image Matrix

With the X-Function mrotate90, you can rotate a matrix 90/180 degrees clockwise or counterclockwise.

// Rotate the matrix 90 degrees clockwize
mrotate90 degree:=cw90;

// Can also use the "matrix" command to rotate matrix 90 degrees
matrix -c r;

Shrink Data Matrix

The X-Function mshrink can shrink a data matrix by specified row and column factors.

// Shrink the active matrix by column factor of 2, and row factor of 1
mshrink cols:=2 rows:=1;

Transpose Data Matrix

The X-Function mtranspose can be used to transpose a matrix.

// Transpose the second matrix object of [MBook1]MSheet1!
mtranspose im:=[MBook1]MSheet1!2;

// Can also use the "matrix" command to transpose a matrix
matrix -t;

Split RGB Image into Separate Channels

The imgRGBsplit X-Functions splits color images into separate R, G, B channels. For example:

// Split channels creating separate matrices for red, green and blue
imgRGBsplit img:=mat(1) r:=mat(2) g:=mat(3) b:=mat(4) colorize:=0;
// Split channels and apply red, green, blue palettes to the result matrices
imgRGBsplit img:=mat(1) r:=mat(2) g:=mat(3) b:=mat(4) colorize:=1;

Please see Image Processing X-Functions for further information on image handling.

Use Temporary Matrix Object as Intermediate Analysis Result

Sometimes user may not want to create a new Matrixbook for output in X-Function each time when performing intermediate matrix analysis operations. He can create a temporary matrix in a hidden Matrixbook for intermediate output, and delete the Matrixbook when it is not needed in next operation.

  • Example 1

This example shows how to save intermediate image operation result in a temporary Matrixbook, and delete it in the next step.

//Import an image into Origin's Matrixbook
string fn$=system.path.program$ + "Samples\Image Processing and Analysis\white camellia.jpg"; 
impImage fname:=fn$;
//Create a hidden Matrixbook
newbook hidden:=1 mat:=1;
%A = bkname$;
//Perform Auto Level operation and save the output in the hidden Matrixbook
imgAutoLevel oimg:=[%A]1! cl:=<optional>;
//Apply Median filter on the image from Auto Level operation
imgMedian d:=3 img:=[%A]1! oimg:=<new>;
//Delete the intermediate matrix
win -cd %A;
  • Example 2

In this example, a median filter was applied on a matrix, and the volume was calculated after the minimum was subtracted. All intermediate matrix results were saved in a hidden temporary Matrixbook, and the Matrixbook was deleted after it was not needed.

//Open a sample Matrixbook
string fn$=system.path.program$ + "Samples\Matrix Conversion and Gridding\2D Gaussian.ogm";
doc -o %(fn$);
//Create a temporary hidden Matrixbook
newbook hidden:=1 mat:=1;
%A = bkname$;
range rm = [%A]1!;
//Add two matrix objects to receive two intermediate matrix results
rm.nmats = 2;
//Apply a median filter on a matrix
medianflt2 n:=3 po:=RepeatPadding om:=[%A]1!mat(1);
//Subtract the minimum
msetvalue im:=[%A]1!mat(2) formula:="mat(1)-z0" script:="double z0; mstats im:=mat(1) min:=z0;";
//Integrate the matrix
double dv;
integ2 im:=[%A]1!mat(2) integral:=dv;
win -cd %A;