Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Skip elements in an aray

Status
Not open for further replies.

chandler

MIS
Dec 10, 2000
66
0
0
US
I've got a routine that computes the mean and standard deviations from an array ($closeData). However, I'd like the routine to skip the the first 97 records. Here is my code that can process the entire array:

Code:
#Function for calculating Standard Deviation
function standard_deviation($std) 
    {
    $total;
    while(list($key,$val) = each($std))
        {
        $total += $val;
        }
    reset($std);
    $mean = $total/count($std);
        
    while(list($key,$val) = each($std))
        { 
        $sum += pow(($val-$mean),2);
        } 
    $var = sqrt($sum/(count($std)-1));
    return $var; 
    }

$stdev = standard_deviation($closeData);

#Loop through array
for ($i = 0; $i < count($closeData); ++$i)
{
$mean_Data[$i]=$closeData[$i];
$up1_Data[$i] = $closeData[$i] + $stdev;
$dn1_Data[$i] = $closeData[$i] - $stdev;
$up2_Data[$i] = $closeData[$i] + $stdev * 2;
$dn2_Data[$i] = $closeData[$i] - $stdev * 2;
}

I'm guessing I need adjust in both the function for standard_deviation adn the for... loop. I've tried manipulating count($std) in the function and $i in the for loop, but nothing as worked so far. Thanks for any help.

Chandler
I ran over my dogma with karma!
 
is this what you were after?

Code:
<?
#Function for calculating Standard Deviation
function standard_deviation($std) 
    {
    $total;
	$cnt = count ($std);
    for ($i=97; $i<$cnt); $i++):
        $total += $str[$i];
    endfor;
	
    $mean = $total/$cnt;
        
	for ($i=97; $i<$cnt); $i++):
	    $sum += pow(($std[$i]-$mean),2);
    endfor;
    $var = sqrt($sum/($cnt-1));
    return $var; 
    }

$stdev = standard_deviation($closeData);

#Loop through array
$cnt = count ($closeData);
for ($i=97; $i < $cnt; $i++):
	$mean_Data[$i]=$closeData[$i];
	$up1_Data[$i] = $closeData[$i] + $stdev;
	$dn1_Data[$i] = $closeData[$i] - $stdev;
	$up2_Data[$i] = $closeData[$i] + $stdev * 2;
	$dn2_Data[$i] = $closeData[$i] - $stdev * 2;
endfor;
?>
 
Another way would be to slice the array before sending it to the function. This would also mean that the function would need to accept a second parameter for the full count of the $closeData array. You can also take out the count() from the 'for loop' and set that to a variable. This will speed up processing because PHP will not have to 'get' the count of the array for each iteration. (Just something to keep in mind.)
Example:
Code:
<?php
#Function for calculating Standard Deviation
function standard_deviation($std, $count)
    {
    $total;
    while(list($key,$val) = each($std))
        {
        $total += $val;
        }
    reset($std);
    $mean = $total/$count;
        
    while(list($key,$val) = each($std))
        {
        $sum += pow(($val-$mean),2);
        }
    $var = sqrt($sum/($count-1));
    return $var;
    }

    
$workingData    = array_slice($closeData, 97);
$closeDataCount = count($closeData);
$stdev          = standard_deviation($workingData, $closeDataCount);

#Loop through array
for ($i = 0; $i < $closeDataCount; ++$i)
{
$mean_Data[$i]=$closeData[$i];
$up1_Data[$i] = $closeData[$i] + $stdev;
$dn1_Data[$i] = $closeData[$i] - $stdev;
$up2_Data[$i] = $closeData[$i] + $stdev * 2;
$dn2_Data[$i] = $closeData[$i] - $stdev * 2;
}
?>
I did not test this code :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top