About Site Map Submit Contact Us Log in | Create an account
Create an account Log In
Average Rating
User Rating:
Visitors Rating:
My rating:

Write review
  • Last update: 5 years ago
  • Total downloads: 33
  • Operating system: Linux, Mac OS X, Win All, BSD, Solaris
  • Publisher: Matt J (15 other programs)
See full specifications

scripts default iconOn-The-Fly Definition of Custom Matrix Objects (Scripts) Publisher's description

This submission defines a generic class of matrix-like objects called MatrixObj and a subclass called DataObj

This submission defines a generic class of matrix-like objects called MatrixObj and a subclass called DataObj. Objects of the class are capable of behaving as matrices, but whose math operators (+,-,*,,.*,,etc...) and other methods can be defined/redefined from within any Mfile or even from the command line. This removes the restriction of writing a dedicated classdef file or class directory for every new matrix-type object that a user might wish to create.

The class works by storing function handles to the various matrix operator functions (plus, minus, mtimes, mldivide, etc...) in a property of MatrixObj called Ops, which is a structure variable. Hence, one can set the matrix operators as desired simply by setting the fields of Ops to an appropriate function handle.

MatrixObj objects are particularly useful when an object needs to be endowed with just a few matrix-like capabilities that are very quickly expressed using anonymous functions or a few short nested functions. This is illustrated in the examples below that deal with creating an efficient version of a DFT matrix. Another advantage of MatrixObj objects is that it is not necessary to issue a "clear classes" command when their Ops methods need to be edited or redefined.

The DataObj subclass is a specialized version of MatrixObj well-suited for mimicking/modifying the behavior of existing MATLAB numeric data types. Its Ops property contains default methods appropriate to existing data types, but which can be selectively overwritten. Example 4 below illustrates its use.

EXAMPLE 1: Implementing fft() in operator form. As is well-known, the operation fft(x) can be represented as a matrix-vector multiplication. If you have the Signal Processing Toolbox, the relevant matrix can be generated using the DFTMTX function. Otherwise, it can be generated as follows,

Q=fft(eye(d)); %DFT matrix - 2500x2500

The operation fft(x) is equivalent to Q*x, but this is a slow way to perform the operation,


tic; y0=Q*x; toc %Elapsed time is 3.595847 seconds.

However, using the MatrixObj class, we can quickly create an object Qobj which can transform x using the same matrix multiplication syntax, Qobj*x, but which uses fft() under the hood, with all of its advantages in speed,

Qobj.Ops.mtimes=@(obj,z) fft(z); %set the mtimes method in 1 line!!

tic; y1=Qobj*x; toc %Elapsed time is 0.212282 seconds.
tic; y2=fft(x); toc %Elapsed time is 0.212496 seconds.

isequal(y1,y2); % =1

And of of course, the memory footprint of Qobj is far less than for the full matrix Q

>>whos Q Qobj

Name Size Bytes Class Attributes

Q 2500x2500 100000000 double complex
Qobj 1x1 4412 MatrixObj

EXAMPLE 2: Continuing with Example 1, suppose I now decide that I still want Qobj to represent an fft() operation, but that it be normalized to satisfy Parseval's theorem. A simple on-the-fly redefinition of mtimes() can accomplish this.

Qobj.Ops.mtimes=@(obj,z) (1/sqrt(numel(z)))*fft(z);


TestParseval=[norm(x), norm(Qobj*x)], % =[28.2807, 28.2807]

EXAMPLE 3: Continuing with Example 2, let us now look at how to give Qobj a ctranspose method so that Qobj' is defined. Because Qobj satisfies Parseval's theorem, Qobj' is its inverse. A one-line definition can be made using the Trans property,

Qobj.Trans.mtimes=@(obj,z) sqrt(numel(z))*ifft(z) ;

The code below verifies that the ctranpose operation has various anticipated properties,

TestParseval=[norm(x), norm(Qobj'*x)], % =[28.2807, 28.2807]

AdjointOfAdjoint=isequal(Qobj*x, (Qobj')'*x), % =1

InversionerrorLeft=norm(x- Qobj'*(Qobj*x)), % =8.4315e-015

InversionerrorRight=norm(x- Qobj*(Qobj'*x)), % =7.9086e-015

EXAMPLE 4:The following is an example of the DataObj subclass. Here, we use it to create a specialized array type which invokes bsxfun() for certain operations. This can be a useful way of circumventing bsxfun's lengthy functional syntax. Other operations like mtimes have default implementations.


P.Ops.minus=@(A,B) bsxfun(@minus,A,B);
P.Ops.plus= @(A,B) bsxfun(@plus,A,B);

S=P*Q, %This uses a natural default

P =

1 3
2 4

Q =

0 1
1 2

R =

4 6
9 11

S =

3 7
4 10

System Requirements:

MATLAB 7.9 (2009b)
Program Release Status: New Release
Program Install Support: Install and Uninstall

On-The-Fly Definition of Custom Matrix Objects (Scripts) Tags:

Click on a tag to find related softwares

Is On-The-Fly Definition of Custom Matrix Objects (Scripts) your software?

Manage your software

Most Popular

scripts default icon ASK, OOK, FSK, BPSK, QPSK, 8PSK modulation 1.1
ASK, OOK, FSK, BPSK, QPSK, 8PSK modulation contain several functions for digital modulation simulation
scripts default icon Simulink Communication Labs 1.1
Simulink Communication Labs allows you to learn communication systems in greater depth.
scripts default icon M-QAM modulation and demodulation 1.1
M-QAM modulation and demodulation is the QAM modulation and demodulation tech.
scripts default icon LZW Compression/Decompression 1.1
LZW Compression/Decompression - Updated LZW compressor and decompressor with reasonable performance
scripts default icon InSPIRE utility to plot a 2D displacement field (Scripts) 1.0
This program plots the deformation field (displace vectors) contained in vector.txt.