help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [help] the mean program in statitistical analysis


From: Jaroslav Hajek
Subject: Re: [help] the mean program in statitistical analysis
Date: Wed, 6 May 2009 07:32:59 +0200

On Wed, May 6, 2009 at 4:18 AM, febty febriani <address@hidden> wrote:
>
>
>>
>> Since this line gets executed in each cycle, you open the file 36000
>> times, which likely causes your problem.
>> Either put a fclose after the following fprintf calls or better open
>> fh2 outside the loop (I see no need to reopen it).
>> Btw., you can probably write a much simpler and faster code by using
>> more vectorization. Also, it seems your code indexes out of bounds in
>> x.
>>
>>
>> *******
>
> thanks very much for your response. it is very useful for me. I fixed my
> program based on your clue. I tried  to put fh2 outside the loop, but I
> didn't get the output file.

Then you probably messed up something.

> So I put the fh2 inside the loop and added the
> fclose after fprintf and endfor command. Fortunately, I can get the 3600
> lines data in the output file, but I met some error on the terminal.
>
> the error is :
> error: invalid column index = 36010
> error: evaluating assignment expression near line 4, column 3
> error: evaluating for command near line 3, column 1

Obviously. I told you your code indexes out of bounds. when `i' gets
large enough,
i*10 + 10 will be over 36000.

> In my mind, I think the error means the program read input file again after
> the 36000 lines, I have no idea how to stop it.

No, you're wrong. See above.

> Octave is my first program on the linux environment, so I have no idea how
> to make the program much faster and simpler by using more vectorization.
> Maybe, are there the best links or websites for me to learn about it by
> myself?

http://lmgtfy.com/?q=octave+manual


> Thanks very much in advance.
>
> This my complete program :
>
> #!/bin/bash
>
> for year in 2008
> do
> for month in 08
> do
> for day in 26
> do
> for hour in 01
> do
> for min in 00
> do
> for sec in 00
> do
>
> octave -q <<EOF
> fid=fopen("${year}${month}${day}${hour}${min}${sec}.dat","r");
> #x=fscanf(fid,"%lf", [8 36000]);
> x=fwrite(fid,"%lf", [8, 36000]);
>
> for i=0:36000
> x2=x(2,i*10+1:i*10+10);
> x3=x(3,i*10+1:i*10+10);
> x4=x(4,i*10+1:i*10+10);
> x5=x(5,i*10+1:i*10+10);
> x6=x(6,i*10+1:i*10+10);
> x7=x(7,i*10+1:i*10+10);
> x8=x(8,i*10+1:i*10+10);
>
> x9=mean(x2);
> x10=mean(x3);
> x11=mean(x4);
> x12=mean(x5);
> x13=mean(x6);
> x14=mean(x7);
> x15=mean(x8);
>
> fh2=fopen("${year}${month}${day}${hour}${min}${sec}.1Hzc.dat","a+");
> fprintf(fh2,'%f',x9);
> fprintf(fh2,'%10f',x10);
> fprintf(fh2,'%10f',x11);
> fprintf(fh2,'%10f',x12);
> fprintf(fh2,'%10f',x13);
> fprintf(fh2,'%10f',x14);
> fprintf(fh2,'%10f\n',x15);
> fclose(fh2);
>
> endfor;
> fclose(fid);
> EOF
>
> done
> done
> done
> done
> done
> done
>
>
>

without actually testing, I think your code can be simplified like this:

fid=fopen("${year}${month}${day}${hour}${min}${sec}.dat","r");
x=fscanf(fid,"%lf", [8 36000]);
fclose(fid);

x = reshape (x, 8, 10, []);
x = mean (x, 2);

fh2=fopen("${year}${month}${day}${hour}${min}${sec}.1Hzc.dat","w");
fmt = [repmat("%10f ", 1, 10), "\n"];
fprintf (fh2, fmt, x);
fclose (fh2);

If you don't want to hard-code the number of lines, write Inf instead
of 36000, that should read the entire input file.

Btw. you should better avoid starting Octave repeatedly in a script
(if possible), because it takes a non-trivial time. It may be
beneficial to put even the outer bash-level loop inside the Octave
code.

-- 
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]