5

I will try to explain the problem that I have with this code.

This script works well to up to three persons ($numRows = 3).

$z=0;
$i=0;
$x=0;

do {
    $total[] = (
        ${'contaH'.$z}[$i+0]*$final[$x+0]+
        ${'contaH'.$z}[$i+1]*$final[$x+1]+
        ${'contaH'.$z}[$i+2]*$final[$x+2]
    );
    $z++;
} while ($z<$numRows); //3

But if I have only four persons ($numRows = 4), I need something like this:

$z=0;
$i=0;
$x=0;

do {
    $total[] = (
        ${'contaH'.$z}[$i+0]*$final[$x+0]+
        ${'contaH'.$z}[$i+1]*$final[$x+1]+
        ${'contaH'.$z}[$i+2]*$final[$x+2]+
        ${'contaH'.$z}[$i+3]*$final[$x+3]
        // if they are 5 persons ($numRows=5), here, should exists another row
    );
    $z++;
} while ($z<$numRows); //4

So the problem is to automate these changes in relation of $numRows.

Here is a demo of matrix algebra:

Enter image description here

The only thing that I want is put my code dynamically in a function of number of persons.

A   |  B |  C |  D
Person1
Person2
Person3
Person4
...

What can be different in my case is just the number of persons.

More information here.

9
  • your Question is totally unclear. please re write your question Commented Oct 29, 2011 at 2:30
  • Please describe the problem in more general terms, trying to figure out what you want to do from a piece of overcomplicated code isn't easy. What's your data structure? Do you have several variables $contaH0, $contaH1 etc? Why aren't you using arrays instead? Commented Oct 29, 2011 at 2:31
  • there is no problem with the code above. The only thing that i want to implement is a variable number of sum rows. if number of $numRows are 3, so must have three sums for each loop, if 4, must have four sums, if 5, must have 5 sums. What must change is the number of sums in function of $numRows Commented Oct 29, 2011 at 2:42
  • Still, please state what you want to accomplish. The code you are posting is... strange, to say the least. Commented Oct 29, 2011 at 2:46
  • 2
    $total[] = () should have a variable number of sums. $total[] = (1), $total[] = (1+1), $total[] = (1+1+1), $total[] = (1+1+1+1), $total[] = (1+1+1+1+1). The number of sums change in function of $numRows . i can't be more clear :S Commented Oct 29, 2011 at 2:51

3 Answers 3

2
$z=0;
$i=0;
$x=0;
$numRows = 5;

do{
    $currentSum = 0;
    for($c = 0; $c < $numRows; $c++){
        $currentSum += (${'contaH'.$z}[$i+$c] * $final[$x+$c]);
    }
    $total[] = $currentSum;
    $z++;
}while($z < $numRows);
Sign up to request clarification or add additional context in comments.

Comments

0
$subtotal = 0;
for ($i = 0; $i < $numRows; $i++) {
   $subtotal += ${'contaH'.$z}[$i] * $final[$i];
}
$total[] = $subtotal;

1 Comment

thanks again. the output will be ( [0] => 313.76656746 [1] => 0 [2] => 0 [3] => 0 ) [1] => 0 ). And the correct should be something like this : ( [0] => 0.320670055732 [1] => 0.324886219083 [2] => 0.19494002706 [3] => 0.159503698125 ) thanks for the effort
0

You might be interested in the Math_Matrix library which will help you do all sorts of matrix arithmetic.

The following code, however, automates your solution:

function mat_mult($matrix, $vector) {
    $result = array();
    $matrixWidth = count($matrix[0]);
    for ($z = 0; $z < $matrixWidth; $z++) {
        $value = 0;
        for ($y = 0; $y < $matrixWidth; $y++) {
            $value += $matrix[$z][$y]*$vector[$y];
        }
        $result[] = $value;
    }
    return $result;
}

$matrix = array(
    array(1, 1/3.0, 2, 4),
    array(3, 1, 5, 3),
    array(1/2.0, 1/5.0, 1, 1/3.0),
    array(1/4.0, 1/3.0, 3, 1)
);
$vector = array(0.26, 0.50, 0.09, 0.16);

$v2 = mat_mult($matrix, $vector);

print_r($v2);

Also, to tie it more into your existing matrix structure:

$matrix = array();
for ($z = 0; $z < $numRows; $z++) {
    $matrix[] = ${'contaH'.$z};
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.