comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Oooh...! It's harder than I thought!! To get average of each line using only meaningful data
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Oooh...! It's harder than I thought!! To get average of each line using only meaningful data [message #54955 is a reply to message #54954] Tue, 24 July 2007 01:32 Go to previous message
Ingo von Borstel is currently offline  Ingo von Borstel
Messages: 54
Registered: September 2006
Member
Hello,

> test00.txt
> 0 2 4 6 7 0
> 0 0 3 3 2 0
> 1 2 3 4 5 0
> 0 0 0 1 2 0
>
> I need to get the average of each line.
> For example, I have 4 meaningful data in the first line. In this case
> I want to have 4.75 (19/4) instead of 3.167 (19/6).
>
> To do this
> 1. I need to get the number of meaningful data, n, in each line first.
> 2. summation of each line, sum.
> 3. sum/n..., and then print to output.txt
> That's all I need. I believe I can handle step 2 and 3, but step 1 is
> harder than I thought...
>
> Please give me any suggestions. Thank you !!!

Assuming you have a fixed amount of data per line, it should work
somewhere along these lines:

data_arr = dblarr(N)
OPEN, in_lun, 'test00.txt', /GET_LUN
OPENW, out_lun, 'output.txt', /GET_LUN
WHILE NOT EOF(in_lun) DO BEGIN
; read all your data, also the irrelevant, of one line and store them in
data_arr
; something like this:
data_strg = readf(in_lun,format='(a)'
data_strgs = strsplit(data_strg,' ',/EXTRACT)
n_datastrgs = N_ELEMENS(data_strgs)
FOR i=0,n_datastrgs-1 DO data_arr[i] = DOUBLE(data_strgs[i])

; now get the relevant ones
where_notzero = WHERE(data_arr NE 0,n_notzero)
average = total(data_arr) / n_notzero

; write away your data to output.txt
PRINTF, out_lun, STRING(average,FORMAT='(f10.5)')

ENDWHILE
CLOSE, out_lun
CLOSE, in_lun

Best regards,
Ingo

--
Ingo von Borstel <newsgroups@planetmaker.de>
Public Key: http://www.planetmaker.de/ingo.asc

If you need an urgent reply, replace newsgroups by vgap.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: IDL crashing on call of TV - solved
Next Topic: Re: recommendations for curve fitting

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 03:22:42 PDT 2025

Total time taken to generate the page: 2.08130 seconds