23-Emerald V
January 14, 2010
Question
aggregate functions - any, all & other Matlab fns
- January 14, 2010
- 2 replies
- 7329 views
On 1/14/2010 10:57:52 AM, adiaz wrote:
== Reading another programs syntax, it could be interesting implementing any(M,r), where M is a matrix and r a row (0 for the entire matrix), and the "inverse" function all; that's from matlab.
See http://collab.mathsoft.com/read?132395,63
Putting aside symbolic considerations for the moment, here's one possibility for implementing 'any' and 'all'.
Note that Mathcad tends to exhibit slightly different behaviour from Matlab (apart from the ORIGIN difference).
For example, Matlab's max function takes a variable number of arguments, max(A,dim) or max(A), where A may be a multi-dimensional array and dim specifies which dimension to find the 'dimension' maxima along. If dim isn't specified then Matlab treats the values along the first non-singleton dimension as vectors, returning the maximum value of each vector; for a standard 2D matrix, this will across the columns.
Mathcad, on the other hand, applies max to the array as a whole and doesn't support MDAs with more than 2 dimensions. Furthermore, Mathcad doesn't support user-defined functions that accept a variable number of arguments.
Consequently, I decided to implement (lower case) 'any' and 'all' in keeping with the Mathcad style, ie they act on the whole array.
I have further defined (capitalized) 'Any' and 'All' that act like their Matlab counterparts. I've taken care of the ORIGIN difference by letting dim=-1 (a non-existent dimension) to act as Matlab's default column-first behaviour (equivalent to not specifying dim), 0 and 1 represent aggregation down each column, and 2 represents aggregation along each row. This means it should give the same results as Matlab does; in particular, aggregating down each column returns a 1xncols matrix. Mathcad, conventionally, returns single-dimensional arrays as vectors and so would return a column aggregation as an ncols vector, so I have chosen to let dim=0 return the result in this form.
It was a fairly quick implementation, and I've only given it a cursory glance over, so there may well be some mistakes in it ... however, it's the thought that counts ... isn't it???
Stuart
== Reading another programs syntax, it could be interesting implementing any(M,r), where M is a matrix and r a row (0 for the entire matrix), and the "inverse" function all; that's from matlab.
See http://collab.mathsoft.com/read?132395,63
Putting aside symbolic considerations for the moment, here's one possibility for implementing 'any' and 'all'.
Note that Mathcad tends to exhibit slightly different behaviour from Matlab (apart from the ORIGIN difference).
For example, Matlab's max function takes a variable number of arguments, max(A,dim) or max(A), where A may be a multi-dimensional array and dim specifies which dimension to find the 'dimension' maxima along. If dim isn't specified then Matlab treats the values along the first non-singleton dimension as vectors, returning the maximum value of each vector; for a standard 2D matrix, this will across the columns.
Mathcad, on the other hand, applies max to the array as a whole and doesn't support MDAs with more than 2 dimensions. Furthermore, Mathcad doesn't support user-defined functions that accept a variable number of arguments.
Consequently, I decided to implement (lower case) 'any' and 'all' in keeping with the Mathcad style, ie they act on the whole array.
I have further defined (capitalized) 'Any' and 'All' that act like their Matlab counterparts. I've taken care of the ORIGIN difference by letting dim=-1 (a non-existent dimension) to act as Matlab's default column-first behaviour (equivalent to not specifying dim), 0 and 1 represent aggregation down each column, and 2 represents aggregation along each row. This means it should give the same results as Matlab does; in particular, aggregating down each column returns a 1xncols matrix. Mathcad, conventionally, returns single-dimensional arrays as vectors and so would return a column aggregation as an ncols vector, so I have chosen to let dim=0 return the result in this form.
It was a fairly quick implementation, and I've only given it a cursory glance over, so there may well be some mistakes in it ... however, it's the thought that counts ... isn't it???
Stuart

