|
Ashok Varma wrote:
Requirement 1: I need ... to get the mean values of few columns(every third column) in a big m*n matrix.
Requirement 2: I want to find the mean of highlighted columns, What I did now is I wrote a program to pull the highlighted columns into a new matrix and then I wrote an another program to find the mean of values in every row.
Requirement 3: I wonder if there is any way to find the mean of intermediate columns using one program <AND> without pulling these values into a new matrix and have an another program to find the mean for all the rows in that matrix.
|
My bold in the your quoted message.
If I was doing this as a one-off task, I'd go either of Fred or Werner's routes... unless I had a ready made set of lower level functions that allowed me to quickly build up the necessary sub-arrays and apply aggregate functions to them. As it happens, I have a few such functions that I've developed over the aeons. I present them here in Mathcad Prime 3.1 format, but they are easily modified for Mathcad 15 (I just wanted to show off the Prime row operator rather than have to transpose arrays to use the column operator or write for loops to get at rows).
Requirement 1 is met by the expression Mean(A,everynthcol(3),1) - or it transpose if you want the result as a vector.
Requirement 2 is met by the expression Mean(A,everynthcol(3),0) - no need for the transpose as it already returns a vector
Requirement 3 is met by the function MyMean(A,3) and returns a 1x2 array with the column means in the first element and the row means in the second element.
The first two functions munif and randmat are the kind of thing I should imagine most people have in their box of tricks; I make frequent use of them (or variants of them)
Averaging is only one of the aggregate functions and I have had occasion to want the variance, standard deviation or even median and mode of both rows and columns. In particular, I've needed them for my Multi Dimensional Array (MDA) library (which extends Mathcad's 2D arrays into a standard hyper-rectangular array system). This latter use calls for the ability to apply one of the aggregate functions along any of the N dimensions of an MDA; for Mathcad standard arrays this is simply a choice of 0 or 1 (assuming you've got ORIGIN = 0). I then write a few aggregate specific functions, such as Mean and Var shown below. Getting column or row averages is a very common task, so it makes it worthwhile having these functions in your toolbox.
The next item on the agenda is to pick out the rows and columns that are wanted. To do this I've used the functions pickcols and pickrows, that take an array and an arbitrary list of column/row numbers, and then return the desired columns/rows. These two functions are then coupled with function everynth to produce everynthcol and everynthrow that return every nth column/row of an array. (I use everynth (or, rather, something similar) as part of my MDA library to iterate along a specific dimension).
Finally, MyMean combines everynthcol and Mean to calculate the means of the extracted columns and their rows. As I said, it's not the kind of thing you'd probably want to write from scratch, but if you have happen to have the bits lying around, or are the kind of person who hates to have write something specific when general will do, then it may well be a viable approach (All *I* had to do was write MyMean ... the rest I already had).
[I normally work with ORIGIN = 0, but I'm rewriting my standard routines to accommodate different origins (so I can do some cross-checking with Matlab functions). However, to paraphrase Fermat, I didn't have room in the margin to make pickcols, etc origin-independent. I'm trying to emulate Werner by putting as many regions into as little space as possible and keep it all on one page!
]

Stuart