Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Need help for calculating mean value under specific condition

Subject: Need help for calculating mean value under specific condition

From: Kostas

Date: 15 Sep, 2010 19:45:21

Message: 1 of 7

Hello, i need your help for calculating the mean value of the group of measurements. More specific a measurement with the instrument i work consists of 5 repetition and i want to calculate the mean value of those 5 repetitions, standard deviation etc. Next few lines indicated an example of my data

gr gn t0 mu par1 par2 fil t1 dt
3 2 467.89 3.1825 255.67 -4.3011 0 8 42
3 3 468.52 3.1711 256.24 1.9434 0 8 42
3 4 469.14 3.16 268.22 1.8474 0 8 42
3 5 469.77 3.1488 274.77 1.8181 0 8 42
4 1 471.47 3.1192 273.92 1.6576 0 8 42
4 2 472.09 3.1086 273.86 0.51999 0 8 42
4 3 472.72 3.0979 259.47 0.69163 0 8 42
4 4 473.35 3.0874 262.09 1.7747 0 8 42
4 5 473.98 3.0769 275.56 1.667 0 8 42
5 1 511.87 2.6065 268.39 1.7885 0 9 42
5 2 512.5 2.6008 267.35 2.0987 0 9 42
5 3 513.13 2.5951 267.64 1.6207 0 9 42
5 4 513.76 2.5895 268.9 1.4998 0 9 42
5 5 514.39 2.584 267.78 1.5686 0 9 42

I am working in basic and until now i was doing what i need like this
        
While Not EOF(7)
'read the parameters line by line
Input #7, gr, gn, O3, SO2, t0, mu, fil, temp, dt
'create the arrays and sums
            n3(gr) = n3(gr) + 1: smu(gr) = smu(gr) + mu: sdt(gr) = sdt(gr) + dt
            st0(gr) = st0(gr) + t0: spar1(gr) = spar1(gr) + par1: Spar2(gr) = Spar2(gr) + par2: spar12(gr) = spar12(gr) + par1 ^ 2: sfil(gr) = sfil(gr) + fil: stemp(gr) = stemp(gr) + temp
Wend
        
        For i = 1 To gr
            If n3(i) = 5 Then ->This is the specific condition i need
            mt0 = st0(i) / n3(i)
            mpar1 = spar1(i) / n3(i): mpar2 = Spar2(i) / n3(i)
            stpar1 = Sqr((spar12(i) / n3(i) - mpar1 ^ 2))
            serror = 2 * stpar1 / Sqr(n3(i))
            mfil = sfil(i) / n3(i)
            airm = smu(i) / n3(i): mtemp = stemp(i) / n3(i)
            mdt = sdt(i) / n3(i)
            Print #8, i; mt0; airm; mpar1; mpar2; stpar1; serror; mfil; mtemp; mdt
            End If
        Next i

The problem is that for further data analysis or plots i need to use other software packages, so i was wondering if someone could be kind enough to help me create a matlab script, so as to do my whole analysis in matlab. Thanks in advance

Subject: Need help for calculating mean value under specific condition

From: dpb

Date: 15 Sep, 2010 19:56:55

Message: 2 of 7

Kostas wrote:
> Hello, i need your help for calculating the mean value of the group of
> measurements. More specific a measurement with the instrument i work
> consists of 5 repetition and i want to calculate the mean value of those
> 5 repetitions, standard deviation etc. Next few lines indicated an
> example of my data
>
> gr gn t0 mu par1 par2
> fil t1 dt
> 3 2 467.89 3.1825 255.67 -4.3011 0 8 42
> 3 3 468.52 3.1711 256.24 1.9434 0 8 42
> 3 4 469.14 3.16 268.22 1.8474 0 8 42
> 3 5 469.77 3.1488 274.77 1.8181 0 8 42
> 4 1 471.47 3.1192 273.92 1.6576 0 8 42
> 4 2 472.09 3.1086 273.86 0.51999 0 8 42
...

> The problem is that for further data analysis or plots i need to use
> other software packages, so i was wondering if someone could be kind
> enough to help me create a matlab script, so as to do my whole analysis
> in matlab. Thanks in advance

In general, "no"...c.s-s.m respondents will bend over backwards often to
help but rarely actually write solutions w/o the requestor showing what
efforts they've made on their own...

So, in that vein,

Begin by reading the "Getting Started" tutorial information in the
online documentation -- it doesn't take long to begin to get a feel for
how Matlab works.

Once you've got that, following specifics may be of benefit...

Your data is pretty regular, look at

doc textscan

or the import wizard for getting the data into Matlab for working on.

doc mean
doc std

will give you the tools for the computations.

doc unique

and reading on logical addressing will provide some ideas on how to
segregate the data by measurement group.

Good luck; feel free to post specific questions (show your work) as you
progress...

--

Subject: Need help for calculating mean value under specific condition

From: Kostas

Date: 15 Sep, 2010 20:44:22

Message: 3 of 7

dpb <none@non.net> wrote in message <i6r8lc$ac1$1@news.eternal-september.org>...
> Kostas wrote:
> > Hello, i need your help for calculating the mean value of the group of
> > measurements. More specific a measurement with the instrument i work
> > consists of 5 repetition and i want to calculate the mean value of those
> > 5 repetitions, standard deviation etc. Next few lines indicated an
> > example of my data
> >
> > gr gn t0 mu par1 par2
> > fil t1 dt
> > 3 2 467.89 3.1825 255.67 -4.3011 0 8 42
> > 3 3 468.52 3.1711 256.24 1.9434 0 8 42
> > 3 4 469.14 3.16 268.22 1.8474 0 8 42
> > 3 5 469.77 3.1488 274.77 1.8181 0 8 42
> > 4 1 471.47 3.1192 273.92 1.6576 0 8 42
> > 4 2 472.09 3.1086 273.86 0.51999 0 8 42
> ...
>
> > The problem is that for further data analysis or plots i need to use
> > other software packages, so i was wondering if someone could be kind
> > enough to help me create a matlab script, so as to do my whole analysis
> > in matlab. Thanks in advance
>
> In general, "no"...c.s-s.m respondents will bend over backwards often to
> help but rarely actually write solutions w/o the requestor showing what
> efforts they've made on their own...
>
> So, in that vein,
>
> Begin by reading the "Getting Started" tutorial information in the
> online documentation -- it doesn't take long to begin to get a feel for
> how Matlab works.
>
> Once you've got that, following specifics may be of benefit...
>
> Your data is pretty regular, look at
>
> doc textscan
>
> or the import wizard for getting the data into Matlab for working on.
>
> doc mean
> doc std
>
> will give you the tools for the computations.
>
> doc unique
>
> and reading on logical addressing will provide some ideas on how to
> segregate the data by measurement group.
>
> Good luck; feel free to post specific questions (show your work) as you
> progress...
>
> --

Thank you for your reply. I totally agree with your opinion/way of thinking. To be honestly i have a very little matlab experience, like i said before i use it mostly for doing plots. To insert the data isn't a problem for me, actually is pretty easy

filename=['myfile.dat'];
fid=fopen(filename);
data=fscanf(fid,'%g %g',[9 inf]);

my main problem is to "think" a "logical" condition so as to do my calculations under this specific condition, possible because i have learn programming in a very different way than the matlab (i guess, you can understand have a look in my code).

So the values column data(:,2) ranges from 1 to 5 and while column data(:,1) is increasing. What i was thinking is that i could go through a for loop into the whole matrix

for i=1:size(data,2)
    n=n+1;par1=data(:,i);
        if data(i,2)=5
            mval=mean(par1);std1=std(par1);
            fwrite(results....);
            n=0;par1=zeros(5,1);
        end
end

but i think this isn't the most efficient way for programming in matlab and moreover i would like to get more familiar with the matlab way and get ideas.

Subject: Need help for calculating mean value under specific condition

From: Sean

Date: 15 Sep, 2010 21:10:20

Message: 4 of 7

"Kostas " <kfragkos@gmail.com> wrote in message <i6rb76$3dj$1@fred.mathworks.com>...
> dpb <none@non.net> wrote in message <i6r8lc$ac1$1@news.eternal-september.org>...
> > Kostas wrote:
> > > Hello, i need your help for calculating the mean value of the group of
> > > measurements. More specific a measurement with the instrument i work
> > > consists of 5 repetition and i want to calculate the mean value of those
> > > 5 repetitions, standard deviation etc. Next few lines indicated an
> > > example of my data
> > >
> > > gr gn t0 mu par1 par2
> > > fil t1 dt
> > > 3 2 467.89 3.1825 255.67 -4.3011 0 8 42
> > > 3 3 468.52 3.1711 256.24 1.9434 0 8 42
> > > 3 4 469.14 3.16 268.22 1.8474 0 8 42
> > > 3 5 469.77 3.1488 274.77 1.8181 0 8 42
> > > 4 1 471.47 3.1192 273.92 1.6576 0 8 42
> > > 4 2 472.09 3.1086 273.86 0.51999 0 8 42
> > ...
> >
> > > The problem is that for further data analysis or plots i need to use
> > > other software packages, so i was wondering if someone could be kind
> > > enough to help me create a matlab script, so as to do my whole analysis
> > > in matlab. Thanks in advance
> >
> > In general, "no"...c.s-s.m respondents will bend over backwards often to
> > help but rarely actually write solutions w/o the requestor showing what
> > efforts they've made on their own...
> >
> > So, in that vein,
> >
> > Begin by reading the "Getting Started" tutorial information in the
> > online documentation -- it doesn't take long to begin to get a feel for
> > how Matlab works.
> >
> > Once you've got that, following specifics may be of benefit...
> >
> > Your data is pretty regular, look at
> >
> > doc textscan
> >
> > or the import wizard for getting the data into Matlab for working on.
> >
> > doc mean
> > doc std
> >
> > will give you the tools for the computations.
> >
> > doc unique
> >
> > and reading on logical addressing will provide some ideas on how to
> > segregate the data by measurement group.
> >
> > Good luck; feel free to post specific questions (show your work) as you
> > progress...
> >
> > --
>
> Thank you for your reply. I totally agree with your opinion/way of thinking. To be honestly i have a very little matlab experience, like i said before i use it mostly for doing plots. To insert the data isn't a problem for me, actually is pretty easy
>
> filename=['myfile.dat'];
> fid=fopen(filename);
> data=fscanf(fid,'%g %g',[9 inf]);
>
> my main problem is to "think" a "logical" condition so as to do my calculations under this specific condition, possible because i have learn programming in a very different way than the matlab (i guess, you can understand have a look in my code).
>
> So the values column data(:,2) ranges from 1 to 5 and while column data(:,1) is increasing. What i was thinking is that i could go through a for loop into the whole matrix
>
> for i=1:size(data,2)
> n=n+1;par1=data(:,i);
> if data(i,2)=5
> mval=mean(par1);std1=std(par1);
> fwrite(results....);
> n=0;par1=zeros(5,1);
> end
> end
>
> but i think this isn't the most efficient way for programming in matlab and moreover i would like to get more familiar with the matlab way and get ideas.

I can't really interpret what you're trying to do.
Since you clearly care about data in the second column == 5 why don't you find those first and then operate only on the (rows?) that contain it? Once again, the description is vague and the sample code is impossible to interpret. I tried, and couldn't wrap my head around what you were trying to do.

rows = find(data(:,2) ==5);
for ii = 1:numel(rows);
    results = calculate_your_results(data(rows(ii),:))
    fprintf(results)
end

Subject: Need help for calculating mean value under specific condition

From: Kostas

Date: 15 Sep, 2010 21:51:04

Message: 5 of 7

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6rcns$dip$1@fred.mathworks.com>...
> "Kostas " <kfragkos@gmail.com> wrote in message <i6rb76$3dj$1@fred.mathworks.com>...
> > dpb <none@non.net> wrote in message <i6r8lc$ac1$1@news.eternal-september.org>...
> > > Kostas wrote:
> > > > Hello, i need your help for calculating the mean value of the group of
> > > > measurements. More specific a measurement with the instrument i work
> > > > consists of 5 repetition and i want to calculate the mean value of those
> > > > 5 repetitions, standard deviation etc. Next few lines indicated an
> > > > example of my data
> > > >
> > > > gr gn t0 mu par1 par2
> > > > fil t1 dt
> > > > 3 2 467.89 3.1825 255.67 -4.3011 0 8 42
> > > > 3 3 468.52 3.1711 256.24 1.9434 0 8 42
> > > > 3 4 469.14 3.16 268.22 1.8474 0 8 42
> > > > 3 5 469.77 3.1488 274.77 1.8181 0 8 42
> > > > 4 1 471.47 3.1192 273.92 1.6576 0 8 42
> > > > 4 2 472.09 3.1086 273.86 0.51999 0 8 42
> > > ...
> > >
> > > > The problem is that for further data analysis or plots i need to use
> > > > other software packages, so i was wondering if someone could be kind
> > > > enough to help me create a matlab script, so as to do my whole analysis
> > > > in matlab. Thanks in advance
> > >
> > > In general, "no"...c.s-s.m respondents will bend over backwards often to
> > > help but rarely actually write solutions w/o the requestor showing what
> > > efforts they've made on their own...
> > >
> > > So, in that vein,
> > >
> > > Begin by reading the "Getting Started" tutorial information in the
> > > online documentation -- it doesn't take long to begin to get a feel for
> > > how Matlab works.
> > >
> > > Once you've got that, following specifics may be of benefit...
> > >
> > > Your data is pretty regular, look at
> > >
> > > doc textscan
> > >
> > > or the import wizard for getting the data into Matlab for working on.
> > >
> > > doc mean
> > > doc std
> > >
> > > will give you the tools for the computations.
> > >
> > > doc unique
> > >
> > > and reading on logical addressing will provide some ideas on how to
> > > segregate the data by measurement group.
> > >
> > > Good luck; feel free to post specific questions (show your work) as you
> > > progress...
> > >
> > > --
> >
> > Thank you for your reply. I totally agree with your opinion/way of thinking. To be honestly i have a very little matlab experience, like i said before i use it mostly for doing plots. To insert the data isn't a problem for me, actually is pretty easy
> >
> > filename=['myfile.dat'];
> > fid=fopen(filename);
> > data=fscanf(fid,'%g %g',[9 inf]);
> >
> > my main problem is to "think" a "logical" condition so as to do my calculations under this specific condition, possible because i have learn programming in a very different way than the matlab (i guess, you can understand have a look in my code).
> >
> > So the values column data(:,2) ranges from 1 to 5 and while column data(:,1) is increasing. What i was thinking is that i could go through a for loop into the whole matrix
> >
> > for i=1:size(data,2)
> > n=n+1;par1=data(:,i);
> > if data(i,2)=5
> > mval=mean(par1);std1=std(par1);
> > fwrite(results....);
> > n=0;par1=zeros(5,1);
> > end
> > end
> >
> > but i think this isn't the most efficient way for programming in matlab and moreover i would like to get more familiar with the matlab way and get ideas.
>
> I can't really interpret what you're trying to do.
> Since you clearly care about data in the second column == 5 why don't you find those first and then operate only on the (rows?) that contain it? Once again, the description is vague and the sample code is impossible to interpret. I tried, and couldn't wrap my head around what you were trying to do.
>
> rows = find(data(:,2) ==5);
> for ii = 1:numel(rows);
> results = calculate_your_results(data(rows(ii),:))
> fprintf(results)
> end

Sean, first of all i would like to apologize for my bad English. i 'll try to explain a little better what i want to do giving an example
 
 3 1 467.47 3.1192 273.92 1.6576 0 8 42
 3 2 467.89 3.1825 255.67 -4.3011 0 8 42
 3 3 468.52 3.1711 256.24 1.9434 0 8 42
 3 4 469.14 3.16 268.22 1.8474 0 8 42
 3 5 469.77 3.1488 274.77 1.8181 0 8 42
 4 1 471.47 3.1192 273.92 1.6576 0 8 42

if you see while the first column is 3 second takes values from 1 to 5 (so like this there is a "group" of 5 values for the index 3), after first column take the value 4 and second again from 1 to 5 (then a "group" for the index 4) and so on. What i want to do is to calculate the mean value let's say of the 5th column while the value of the first is steady (the mean value of each group).

eg while first column = 3, average = (273.92+255.67 +256.24+268.22+274.77)/5=....
when first column = 4, average=(a1+a2+a3+a4+a5)/5 and so on.

Hope this is little more clear.

Subject: Need help for calculating mean value under specific condition

From: dpb

Date: 15 Sep, 2010 22:47:54

Message: 6 of 7

Kostas wrote:
...

> try to explain a little better what i want to do giving an example
>
> 3 1 467.47 3.1192 273.92 1.6576 0 8 42
> 3 2 467.89 3.1825 255.67 -4.3011 0 8 42
> 3 3 468.52 3.1711 256.24 1.9434 0 8 42
> 3 4 469.14 3.16 268.22 1.8474 0 8 42
> 3 5 469.77 3.1488 274.77 1.8181 0 8 42
> 4 1 471.47 3.1192 273.92 1.6576 0 8 42
>
> if you see while the first column is 3 second takes values from 1 to 5
> (so like this there is a "group" of 5 values for the index 3), after
> first column take the value 4 and second again from 1 to 5 (then a
> "group" for the index 4) and so on. What i want to do is to calculate
> the mean value let's say of the 5th column while the value of the first
> is steady (the mean value of each group).
...

Again,

doc unique % specifically what does u=unique(x(:,1)) give you?

Now, with that, what could you do with something like

for idx=1:length(u)
   m=mean(x(x(:,1)=u(idx),5)
end

do you suppose?

One can, of course, get more clever, but that should be a starting point
kind of hint...

--

Subject: Need help for calculating mean value under specific condition

From: Kostas

Date: 15 Sep, 2010 23:20:25

Message: 7 of 7

dpb <none@non.net> wrote in message <i6rilu$j2r$1@news.eternal-september.org>...
> Kostas wrote:
> ...
>
> > try to explain a little better what i want to do giving an example
> >
> > 3 1 467.47 3.1192 273.92 1.6576 0 8 42
> > 3 2 467.89 3.1825 255.67 -4.3011 0 8 42
> > 3 3 468.52 3.1711 256.24 1.9434 0 8 42
> > 3 4 469.14 3.16 268.22 1.8474 0 8 42
> > 3 5 469.77 3.1488 274.77 1.8181 0 8 42
> > 4 1 471.47 3.1192 273.92 1.6576 0 8 42
> >
> > if you see while the first column is 3 second takes values from 1 to 5
> > (so like this there is a "group" of 5 values for the index 3), after
> > first column take the value 4 and second again from 1 to 5 (then a
> > "group" for the index 4) and so on. What i want to do is to calculate
> > the mean value let's say of the 5th column while the value of the first
> > is steady (the mean value of each group).
> ...
>
> Again,
>
> doc unique % specifically what does u=unique(x(:,1)) give you?
>
> Now, with that, what could you do with something like
>
> for idx=1:length(u)
> m=mean(x(x(:,1)=u(idx),5)
> end
>
> do you suppose?
>
> One can, of course, get more clever, but that should be a starting point
> kind of hint...
>
> --

Thanks again dbp, i think i got your point. i 'll try to have a little more practice on it.

Tags for this Thread

No tags are associated with this thread.

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.

Contact us