|
%elem returns the number of defined elements (10 in your case). If you
only want to use the *first* N array elements then you need the following
changes:
dData s 5p 3 dim(10)
dMean s 5p 3
dStdDev s 8f
dN s 10u 0 inz(4)
/free
Mean = %xfoot(%subarr(Data :1 :N)) / N;
StdDev = %sqrt(%xfoot(((%subarr(Data :1 :N) - Mean) ** 2)) / N);
/end-free
With this change only elements 1 through N (4 as N is currently
initialized above) will be used in the calculation.
Bruce Vining
On Sun, Mar 25, 2012 at 2:34 PM, James Newman, CDP <newmanas400@xxxxxxxxx>wrote:
Bruce,
Looks to me like that would work. I have to admit I'm not very familiar
with the "free" functions and have never used %elem before. If you use it
on a 10 element array and only have data in 4, what number does it return?
Thanks so much for the help...now just have to wrap the rest of the code
around it.
Jim
On Sun, Mar 25, 2012 at 8:10 AM, Bruce Vining <bvining@xxxxxxxxxxxxxxx
wrote:
I can't say that I've done it prior to this morning, but the followinghere"
appears to work in a quick and very simple (read "disclaimer inserted
as my last standard deviation was in college econ stats -- quite a fewbase on
years ago) test case. I would recommend additional testing, obviously
changing to the precision you really have/need, etc...):
dData s 5p 3 dim(10)
dMean s 5p 3
dStdDev s 8f
/free
Mean = %xfoot(Data) / %elem(Data);
StdDev = %sqrt(%xfoot(((Data - Mean) ** 2)) / %elem(Data));
*inlr = *on;
return;
/end-free
As an aside, I would also recommend loading the data array strictly
a file containing "days off" where days off includes weekends. Based onnot
past experience an implementatin loading values based on (not Saturday,
Sunday, and not a Holiday) is (typically the day following the codenewmanas400@xxxxxxxxx
install) followed by a company decision to start working on the last
Saturday of each month due to increased demand...
Bruce Vining
On Sat, Mar 24, 2012 at 6:16 PM, James Newman, CDP <
sakewrote:
Chuck,
Thanks for the expertise and advice. I left out some info for the
ofrequired
brevity...sorry.
I'm at v5r2...Al Barsa used to howl each time I'd tell him that.
The data for the array comes from a table. After performing the
changefunctions, I throw out the oldest record, add a new one, and start the
process all over again - a "moving average" for example. I could
Ithe beginning and ending dates and use SQL to perform the functions.
time,wonder about the efficiency of that versus reading one record at a
Also,updating the array, and performing the calculations on the array.
10.giving SQL a beginning and ending date is problematic, as I don'tconsider
weekends or holidays and would have to account for that somehow.
For this specific application, the number of days considered would be
arrays,
TIA.
James R. Newman, CDP
On Sat, Mar 24, 2012 at 5:40 PM, CRPence <CRPbottle@xxxxxxxxx> wrote:
On 24-Mar-2012 11:27 , James Newman, CDP wrote:
So I have 10 items in an array and I'd like to figure out a mean
(average) and standard deviation. For the mean, I can XFOOT, then
divide by the number of elements in the array. I've only used SQL
with tables - can I use SQL against an array?
The SQL may not be used as easily as one might desire with
capability,for lack of both an ARRAY data type or TABLE(:array_name)
ARRAYbut there are ways. Easier with IBM i 7.1 SQL I presume, with an
muchdata type and associated functions [using a User Defined Type (UDT)
however], but I have neither experience with the support nor read
wasabout the v7r1 array type support; perhaps moot, as the OS release
becomeleft undisclosed, for what might be available to the OP.
Per "10 items", does that imply there will always be a specific
and\or limited number of items below a specific count? What is the
origin and storage location for the data values which eventually
thefunction.elements of an array declared in the program? Irrespective of the
answers to those questions, whatever loads the array could instead
provide the data as row data via an external user defined TABLE
operate.
A limited number of items could be elements of a values-clause [or
similarly selected as UNION set of data], a derived result TABLE,
against which the STDDEV, AVG, and COUNT aggregate functions could
An example using dynamic SQL that works at v5r3 for four values;
though a values row expression, since v5r4, is much prettier than
mailingUNION ALL:
with SD (d8) as
( select cast(? as dec(8)) d8 from qsqptabl
union all select cast(? as dec(8)) d8 from qsqptabl
union all select cast(? as dec(8)) d8 from qsqptabl
union all select cast(? as dec(8)) d8 from qsqptabl
)
select int(avg(d8))
, dec(stddev(d8), 10, 2)
, int(count(*))
from SD
;
open abovePreparedStmt using :e1, :e2, :e3, :e4
; -- :HV e1 to e4 are array[1] to array[4]
Regards, Chuck
--
This is the RPG programming on the IBM i / System i (RPG400-L)
listlistlist
To post a message email: RPG400-L@xxxxxxxxxxxx--
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.
This is the RPG programming on the IBM i / System i (RPG400-L) mailing
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.
--
Regards,
Bruce
www.brucevining.com
www.powercl.com
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing
To post a message email: RPG400-L@xxxxxxxxxxxx--
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.
This is the RPG programming on the IBM i / System i (RPG400-L) mailing
list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.
--
Regards,
Bruce
www.brucevining.com
www.powercl.com
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.