Thread Subject:
Finding mean for changing number of variables

Subject: Finding mean for changing number of variables

From: Danny

Date: 21 Jun, 2012 18:56:07

Message: 1 of 7

Hi, I'm trying to calculate the average PSD from a variable number of tests. I use the "eval" function to rename i number of variables as they are entered. (User enters no of sets, and variables are computed up to that set number using while loops). So I'm wondering how to convert this:

PSDtrimavg(:,i) = mean([PSD1trim(:,i),PSD2trim(:,i),PSD3trim(:,i)],2);

to compute the mean for any number of varialbes (PSDtrim_1...PSDtrim_i) I've tried constructing them into cell arrays, using a while loop and creating a varying length array, but the mean function can't average the correct corresponding columns.

Here's an example from one of my "new variable" loops
while i <= sets
    PSDavg = eval(['PSDAvg_' num2str(i)]);
    PSDtrim = PSDavg(floor(0.5/df+1):floor(30/df+1),:);
    eval(['PSDtrim_' num2str(i) '=PSDtrim;']);
    freqtrim = freq(floor(0.5/df+1):floor(30/df+1));
    i = i + 1;
end

Thanks for any possible help. One solution would be to creata an array of the variable names as a string and using eval to solve that, but I can't output just the variable name.

Subject: Finding mean for changing number of variables

From: Matt J

Date: 21 Jun, 2012 19:33:07

Message: 2 of 7

"Danny " <dmccorm2@nd.edu> wrote in message <jrvqo7$m72$1@newscl01ah.mathworks.com>...
>
>
> I use the "eval" function to rename i number of variables as they are entered.
===========

You're not kidding, are you?

Subject: Finding mean for changing number of variables

From: Matt J

Date: 21 Jun, 2012 19:40:08

Message: 3 of 7

"Danny " <dmccorm2@nd.edu> wrote in message <jrvqo7$m72$1@newscl01ah.mathworks.com>...
>
> I've tried constructing them into cell arrays, using a while loop and creating a varying length array, but the mean function can't average the correct corresponding columns.
=============

Elaborate on this and what you mean by "the mean function can't average the correct corresponding columns". If your data sets existed as cell array elements data{i}
why couldn't you do this

result=cellfun(@mean,data,'uni',0);

Subject: Finding mean for changing number of variables

From: Danny

Date: 21 Jun, 2012 19:47:07

Message: 4 of 7

No, but I did manage to get it to work by creating variable matrices of corresponding columns, and just finding the mean across. Since there are only 5 channels of data, but potentially "infinite" tests. However, I still feel there is a more forward way of doing it.

Subject: Finding mean for changing number of variables

From: Danny

Date: 21 Jun, 2012 19:52:06

Message: 5 of 7

So the way the equation is set up is to average the corresponding columns of each PSD. In other words, it averages column 1 of PSDtrim_1 with column 1 of PSDtrim_2 to make an averaged column, and then proceeds with column 2 of PSDtrim_1... The way it's stored in the cell array is each matrix entered as it's own column. Now the cell array could be anything from {1,2} to {1,99} and the mean function can't select the correct column from each subcell in a while loop. Simply put I might want to average a, b, c, d. but the mean function is only set up to do a,b,c. Another time I might want to do a,b, and mean(a,b,c) wouldn't fix that.

Subject: Finding mean for changing number of variables

From: Matt J

Date: 21 Jun, 2012 20:03:06

Message: 6 of 7

"Danny " <dmccorm2@nd.edu> wrote in message <jrvu16$7ri$1@newscl01ah.mathworks.com>...
>
>
> In other words, it averages column 1 of PSDtrim_1 with column 1 of PSDtrim_2 to make an averaged column, and then proceeds with column 2 of PSDtrim_1...
==============

For that to be possible, all matrices PSDtrim_i would have to be the same size. So then why couldn't you instead store your PSDs into different slices of a 3D array, i.e., so that instead of PSDtrim_i, you would have PSDtrim(:,:,i)

Then, to average together corresponding columns, all you would need to do is

result=mean(PSDtrim,3);

If this still doesn't apply to your situation, I think we need you to show us some miniature examples of PSDtrim_i, for i=1,2,3 and show what you want the output to be.

Subject: Finding mean for changing number of variables

From: Danny

Date: 21 Jun, 2012 20:21:07

Message: 7 of 7


> For that to be possible, all matrices PSDtrim_i would have to be the same size. So then why couldn't you instead store your PSDs into different slices of a 3D array, i.e., so that instead of PSDtrim_i, you would have PSDtrim(:,:,i)
>
> Then, to average together corresponding columns, all you would need to do is
>
> result=mean(PSDtrim,3);
===================================================

That worked perfectly, thank you. Pretty obvious, but for whatever reason I never considered putting them 3D, probably because the original was in 2D and I was just trying to loop it.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mean Danny 21 Jun, 2012 14:59:10
variables Danny 21 Jun, 2012 14:59:10
changing Danny 21 Jun, 2012 14:59:10
dynamic Danny 21 Jun, 2012 14:59:10
matrix Danny 21 Jun, 2012 14:59:10
cell Danny 21 Jun, 2012 14:59:10
array Danny 21 Jun, 2012 14:59:10
eval Danny 21 Jun, 2012 14:59:10
psd Danny 21 Jun, 2012 14:59:10
average Danny 21 Jun, 2012 14:59:10
rssFeed for this Thread

Contact us