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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Mean value of only few columns in a Matrix

avarma
1-Newbie

Mean value of only few columns in a Matrix

Hello,


I need a little help in mathcad program to get the mean values of few columns(every third column) in a big m*n matrix. See  the attached image for description.


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.

I wonder if there is any way to find the mean of intermediate columns using one program without pulling these values into a new matrix and have an another program to find the mean for all the rows in that matrix.

I could have attached my program file here but unfortunately I don’t have MathCAD in PC write now. Curious to get the answer by the time I go to my work tomorrow

Thanks for your help!!


Ashok

1 ACCEPTED SOLUTION

Accepted Solutions
StuartBruff
23-Emerald II
(To:avarma)

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

View solution in original post

7 REPLIES 7
Fred_Kohlhepp
23-Emerald I
(To:avarma)

You can select a single column in a matrix and take the mean of that column.

If you're doing every third column (column 2, 5, 8, etc., then you'd need to create a range variable    i:= 0. .floor(cols(Matrix)/3)

And a second line:  Avg[I  := mean(Matrix<3(I+i)>)   and you'd have a vector with the means.

Werner_E
24-Ruby V
(To:avarma)

See  the attached image for description.

You did not attach any image, nor a worksheet!


You don't write if you need the mean of all third columns altogether or the mean of every column collected in a row vector.

Furthermore you don't write, which version of the Program you are using - real full Mathcad (version 15 or below) or just Prime.


Its also unclear to me why you write that you calculate the means of the rows of the matrix you build from every third column.


Maybe the following helps:


StuartBruff
23-Emerald II
(To:avarma)

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

Here's an M15 version .... note that I've changed the dimension number to accord with my MDA Library (which I haven't transmogrified to Prime yet).  Dimension 0 corresponds to going down a column (this is the normal way that Mathcad orders elements in an array), Dimension 1 corresponds to going across a row.

Stuart

And as I had a few minutes after lunch, I thought I'd bring the two versions into line and add the ORIGIN offset to the pick functions.  Oh, and I'd forgotten that Mathcad already Has Capitalized Var, but as I never use it ....

Stuart

Hello Stuart,

Thank you very much for your response. This is what exactly I was looking for. I'm using Mathcad Prime 3.1. Thanks for your patience in understanding my question,even though it was not clearly described what I need.

Again Thanks a lot.

Have a great day!

Ashok

StuartBruff
23-Emerald II
(To:avarma)

Ashok Varma wrote:

Hello Stuart,

Thank you very much for your response. This is what exactly I was looking for. I'm using Mathcad Prime 3.1. Thanks for your patience in understanding my question,even though it was not clearly described what I need.

Again Thanks a lot.

Have a great day!

Ashok

No worries, Ashok.  Same to you.

Stuart

Top Tags