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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

aggregate functions - any, all & other Matlab fns

StuartBruff
23-Emerald II

aggregate functions - any, all & other Matlab fns

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
12 REPLIES 12
PhilipOakley
5-Regular Member
(To:StuartBruff)

MatLab often annoys me in its view that matrices are
merely sets of column vectors (as per the comment
about max)
I wish it(matlab) were more flexible in that
respect.
And I wish MathCad were likewise flexible in
allowing variable number of arguments etc.

Philip Oakley

Your functions looks to work fine, free of errors, and with the desired beahvior. It's a full work in the sense that it is very complete and easy in follow the logical implementation of the features. But without this extras it is a great work, so, it is a big great complete logical work.

Some few comments in green in the attached. Modifications to Reduce are for work in my mathcad 11, but as side effect the recursion was eliminated.

Regards. Alvaro.

PD: What offuscate (to me) matlab convention is that, even it is well for matrix decomposition opperations, isn't congruent: if vectors are "column vectors", then must to be always like that, and "rows vectores" are not allowed; a row with scalar values must to be a matrix of column vectors with dimension one, but matlab fails in follow their own convention.

Mathcad style, taking matrices as an entity and appliying aggregate functions to the entire structure it's algebraically correct and useful in the practice. A second optional argument can make all functions more usual for those cases that it is required take columns as vectors, but then it is not avaible in usual mathcad nonsense requirement that functions can't have variable number of arguments. To cover this case, the value -2 as second argument is equivalent to the more comfortable and readable none second argument.

On 1/14/2010 8:48:08 PM, adiaz wrote:
== Your functions looks to work fine, free of errors, and with the desired beahvior.

I now believe in Miracles 🙂

== It's a full work in the sense that it is very complete and easy in follow the logical implementation of the features.

Thanks. I've added a bonus Matlab function trapz (I don't have Matlab, so I'm just going from their documentation - http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trapz.html ).

Once again, I've had to make some adjustments to cope with the distinctions between Matlab and Mathcad, including the lack of variable number of arguments in user-defined functions. However, it seems to work - I've even thrown in an example from Mathematica's NIntegrate using the Trapezoidal option.

== Some few comments in green in the attached. Modifications to Reduce are for work in my mathcad 11, but as side effect the recursion was eliminated.

Thanks, Alvaro. I've copied your function into the attached worksheet, retaining your suggestion that -2 serve to act as the lower-case variant does. Would initializing a as 'a0←0' get round the need to use the submatrix function?

Unfortunately, my M11 machine has a bad case of Gremlins and I can only check M13 (work) and M14 (2nd home PC).


== PD: What offuscate (to me) matlab convention is that, even it is well for matrix decomposition opperations, isn't congruent: if vectors are "column vectors", then must to be always like that, and "rows vectores" are not allowed; a row with scalar values must to be a matrix of column vectors with dimension one, but matlab fails in follow their own convention.

Yes, I sometimes get confused. It might be less confusing if Mathcad implemented MDAs.

== Mathcad style, taking matrices as an entity and appliying aggregate functions to the entire structure it's algebraically correct and useful in the practice.

Agreed

=== A second optional argument can make all functions more usual for those cases that it is required take columns as vectors, but then it is not avaible in usual mathcad nonsense requirement that functions can't have variable number of arguments. To cover this case, the value -2 as second argument is equivalent to the more comfortable and readable none second argument.

Yes. I've sometimes had to write several variants of a function, each one taking a different number of arguments.

Stuart

On 1/15/2010 1:17:40 PM, stuartafbruff wrote:

>Would initializing a as 'a0←0' get
>round the need to use the submatrix
>function?

Nope. I don't know if it is particular for my mathcad 11 setup, but I can't assign a scalar to a row or column vector: this is if x:= (1 2 3) then x<1>:=2 give me an error, also if before I traspose the vector x.



Assigning a[k and trasposing for |dim|=!1 works but not for Cumsum or Cumprod: result then is a nesting array.

Nice function trapz!

Regards. Alvaro.

>I don't know if it is particular for my mathcad 11 setup, but I can't assign a scalar to a row or column vector <<br> ______________________

...


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.
___________________

In reference to Stuart last working "Aggregate", what about having an optional reverse indexing, i.e: a mirror indexing. The optional reverse indexing being from an identifier of some kind.



That would duplicate the functionality of the proposal.

jmG

... a letter would make easier for the mirror.
Glad the 3D recognizes the mirror argument !



jmG

On 1/15/2010 10:57:12 PM, jmG wrote:
== In reference to Stuart last working "Aggregate", what about having an optional reverse indexing, i.e: a mirror indexing. The optional reverse indexing being from an identifier of some kind.

Good idea, Jean. Try

Mirror(A,dim):=Reduce(A,dim,reverse)

(the inconsistency is that built-in function reverse doesn't apply to a whole matrix, but rather just the columns, making it slightly less consistent than I'd like. OTOH, it's easy enough to define a new function, mirror, say, that reverses the whole array)

Stuart

On 1/16/2010 11:52:58 AM, stuartafbruff wrote:
>Good idea, Jean. Try
>
>Mirror(A,dim):=Reduce(A,dim,reverse)
>
>(the inconsistency is that built-in
>function reverse doesn't apply to a
>whole matrix, but rather just the
>columns, making it slightly less
>consistent than I'd like. OTOH, it's
>easy enough to define a new function,
>mirror, say, that reverses the whole array
>
>Stuart
_________________________

Your sheet is so massive that my bed time reading clock is missing some hours ! The simplest way to mirror a whole matrix is by reverse indexing the

mirror(A):=submatrix(A, ir, jr, ic, jc)

Very handy for 3D plots.
The programmed version can be adapted.

Thanks for your collaboration, happy new year Stuart.

Jean

On 1/16/2010 1:54:05 PM, jmG wrote:
== Your sheet is so massive that my bed time reading clock is missing some hours ! The simplest way to mirror a whole matrix is by reverse indexing the ...

Here's a cut-down version to let you get more sleep; note that my 'mirror' reflects an array about it's i = -j diagonal.

Stuart

On 1/16/2010 4:46:38 PM, stuartafbruff wrote:
== On 1/16/2010 1:54:05 PM, jmG wrote:
== Your sheet is so massive that my bed time reading clock is missing some hours ! The simplest way to mirror a whole matrix is by reverse indexing the ...
>> Here's a cut-down version to let you get more sleep; note that my 'mirror' reflects an array about it's i = -j diagonal.

And here's the new War and Peace ... with sort added, as well as reverse and mirror.

Stuart

More quick utilities.

jmG
Top Tags