cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

How Do You Do Columnwise Matrix Operations In MathCAD or MathCAD Prime?

DavidNJ
1-Newbie

How Do You Do Columnwise Matrix Operations In MathCAD or MathCAD Prime?

Given an mxn matrix, I would like to create an array of column means and another of column standard deviations. The statstical functions appear to operate over the whole matrix. A related problem would be doing columnwise addition/subtraction of a vector over a matrix.

In MathLab the equivalent would be the bsxfun function: http://www.mathworks.com/help/techdoc/ref/bsxfun.html

In MathLab std and mean also operate columnwise.

An application would be feature normalization where the normalized matrix had values where each element was equivalent to the (element-mean(column))/stdev(column).

Thanks,

David

6 REPLIES 6
StuartBruff
23-Emerald II
(To:DavidNJ)

David Ziegelheim wrote:

Given an mxn matrix, I would like to create an array of column means and another of column standard deviations. The statstical functions appear to operate over the whole matrix. A related problem would be doing columnwise addition/subtraction of a vector over a matrix.

In MathLab the equivalent would be the bsxfun function: http://www.mathworks.com/help/techdoc/ref/bsxfun.html

In MathLab std and mean also operate columnwise.

An application would be feature normalization where the normalized matrix had values where each element was equivalent to the (element-mean(column))/stdev(column).

Thanks,

David

Are the attached any use, David?

I don't have Prime 2, but I do have Prime 1.0 and have a created a version of mean that operates over each dimension of an array in a similar way to Matlab. It is based upon a function Reduce that can be used to generate Mathcad equivalents of several Matlab functions that operate along a specified dimension.

I've got a Mathcad 15 variant as well, which has a lot more functions, but I haven't finished developing or testing it.

Stuart

Reduce allows column-wise or row-wise application of a function. However, how do you to element-wise computation: Xn := X - Reduce(X,1,mean)?

StuartBruff
23-Emerald II
(To:DavidNJ)

David Ziegelheim wrote:

Reduce allows column-wise or row-wise application of a function. However, how do you to element-wise computation: Xn := X - Reduce(X,1,mean)?

Ah, that bit. Unfortunately, Mathcad doesn't have a proper multi-dimensional array capability (I've no idea why not, it's trivially easy to implement). However, in this case, it appears to be just a case of creating a function that emulates singleton expansion and then using the vectorization operator apply 'fun' to the now-additively conformable arrays.

... oh yes, and creating a magic function (I've kept it very simple - odd number squares only).

It's a fairly top-of-the-head thing, so caveat emptor and all that. In fact, there is a 72.936 % chance that I've completely misunderstood the whole thing; it would have been 108 % were it not for the fact that my worksheet seems to agree with the Matlab result. Bug reports gratefully received and cursingly dealt with.

Stuart

If you are writing custom functions and defining your own units, the custom functions and units need to be copied into the front of every worksheet.

Minimally, there should be a way to store them in a directory or better in a database (which would ensure they are valid and provide a management wrapper. The database could be a simple XML file.

I love having equations look like equations. I'm a bit frustrated at needing to add programming to each worksheet for some functionality...and having no way to format long symbolic results so they fit on a page. That last one makes it hard to print or include in another document.

StuartBruff
23-Emerald II
(To:DavidNJ)

David Ziegelheim wrote:

If you are writing custom functions and defining your own units, the custom functions and units need to be copied into the front of every worksheet.

Minimally, there should be a way to store them in a directory or better in a database (which would ensure they are valid and provide a management wrapper. The database could be a simple XML file.

I love having equations look like equations. I'm a bit frustrated at needing to add programming to each worksheet for some functionality...and having no way to format long symbolic results so they fit on a page. That last one makes it hard to print or include in another document.

There are (at least) 2 ways to deal with the problem.

One, which I use in my latest worksheet, is to use Areas; Areas are parts of the worksheet that are enclosed between 2 'markers'. The Areas can be expanded or collapsed, in the latter case contracting the whole Area to just one line and making the coding 'invisible' to the reader. The whole Area can be copied, cut or pasted making it fairly easy to transfer them between worksheets.

The other way is to use a Reference, which is a link to another worksheet that then gets 'included' in the referencing worksheet. The referenced worksheet can be kept under separate configuration control.

To save the effort of copying Areas or inserting References, you could use set up one or more Templates that use whichever combination of Area or Reference is appropriate to or conventient for particular purposes. For example, I include my Utility functions in Areas on my Normal.xmct Template, which means everytime I create a default worksheet, these functions are immediately available for use.

Stuart

wayne
3-Visitor
(To:DavidNJ)

You can include a file reference in the front of a file.
The referenced file can contain user defined functions, programs and units and anything else.

For the particular question, some operations in Mathcad are very easy:

tmp12.gif

I know the following suggestion is not the best; but you can reduce a long symbolic result by copying portions of the solution and assigning them to a new variable, and then assemble the parts. I locate the "live" symbolic result on the right, and reassemble on the printed page. As a safeguard. you could compare numerical results from each. Just in case something changes, can do checks on the right page so only seen when in Mathcad.

Top Tags