1

I having trouble with multidimensional array. I want to get the Results based on calculate from rows and columns

I have array probabilitas like this

Array ( 
    [0] => Array (
        [0] => 0.21739130434783
        [1] => 0.043478260869565
        [2] => 0.23809523809524
        [3] => 0.071428571428571
        [4] => 0.058823529411765
        [5] => 0.20833333333333
    )

    [1] => Array (
        [0] => 0.08695652173913 
        [1] => 0.17391304347826
        [2] => 0.095238095238095
        [3] => 0.17857142857143 
        [4] => 0.23529411764706
        [5] => 0.16666666666667
    )

    [2] => Array (
        [0] => 0.043478260869565
        [1] => 0.17391304347826
        [2] => 0.095238095238095
        [3] => 0.17857142857143
        [4] => 0.11764705882353
        [5] => 0.16666666666667
    )

    [3] => Array (
        [0] => 0.08695652173913
        [1] => 0.17391304347826
        [2] => 0.19047619047619
        [3] => 0.17857142857143
        [4] => 0.23529411764706
        [5] => 0.083333333333333
    )

    [4] => Array (
        [0] => 0.21739130434783
        [1] => 0.17391304347826
        [2] => 0.19047619047619
        [3] => 0.035714285714286
        [4] => 0.11764705882353
        [5] => 0.16666666666667
    )

    [5] => Array (
        [0] => 0.17391304347826
        [1] => 0.17391304347826
        [2] => 0.095238095238095
        [3] => 0.17857142857143
        [4] => 0.11764705882353
        [5] => 0.16666666666667
    )

    [6] => Array (
        [0] => 0.17391304347826
        [1] => 0.08695652173913
        [2] => 0.095238095238095
        [3] => 0.17857142857143
        [4] => 0.11764705882353
        [5] => 0.041666666666667
    ) 
)

if run on browser wil be like this enter image description here

and i want to get the final results like this

countCol1 = 0,94197665
countCol2 = 0,960859982
countCol3 = 0,960555517
countCol4 = 0,948500044
countCol5 = 0,953102906
countCol6 = 0,956259856

but i got error like this

Message: log() expects parameter 1 to be float, array given

what's wrong with my code ? can someone help me?

this my code

$kriteria = [C1,C2,C3,C4,C5,C6];
$alternatif = [ALT1,ALT2,ALT,ALT4,ALT5,ALT6,ALT7];
$nEntropy = array();
for ($i=0;$i<count($kriteria);$i++)
    {
        for ($j=0;$j<count($alternatif);$j++)
            {
                $nEntropy[$j][$i] = 
                    ((-1/log(7)) * 
                    ($probabilitas[$j][$i]*log($probabilitas[$i])) +
                    ($probabilitas[$j][$i]*log($probabilitas[$i])) + 
                    ($probabilitas[$j][$i]*log($probabilitas[$i])) + 
                    ($probabilitas[$j][$i]*log($probabilitas[$i])) + 
                    ($probabilitas[$j][$i]*log($probabilitas[$i])) + 
                    ($probabilitas[$j][$i]*log($probabilitas[$i])) + 
                    ($probabilitas[$j][$i]*log($probabilitas[$i])));
            }
    }

to calculate it I use this formula.

countCol1 = (-1/log(7)) * ((0,217391304*log(0,217391304))+(0,086956522*log(0,086956522))+(0,043478261*log(0,043478261))+(0,086956522*log(0,086956522))+(0,217391304*log(0,217391304))+(0,173913043*log(0,173913043))+(0,173913043*log(0,173913043)))

countCol2 = (-1/log(7)) * ((0,043478261*log(0,043478261))+(0,173913043*log(0,173913043))+(0,173913043*log(0,173913043))+(0,173913043*log(0,173913043))+(0,173913043*log(0,173913043))+(0,173913043*log(0,173913043))+(0,086956522*log(0,086956522)))

countCol3 = (-1/log(7)) * ((0,238095238*log(0,238095238))+(0,095238095*log(0,095238095))+(0,095238095*log(0,095238095))+(0,19047619*log(0,19047619))+(0,19047619*log(0,19047619))+(0,095238095*log(0,095238095))+(0,095238095*log(0,095238095)))

countCol4 = (-1/log(7)) * ((0,071428571*log(0,071428571))+(0,178571429*log(0,178571429))+(0,178571429*log(0,178571429))+(0,178571429*log(0,178571429))+(0,035714286*log(0,035714286))+(0,178571429*log(0,178571429))+(0,178571429*log(0,178571429)))

countCol5 = (-1/log(7)) * ((0,058823529*log(0,058823529))+(0,235294118*log(0,235294118))+(0,117647059*log(0,117647059))+(0,235294118*log(0,235294118))+(0,117647059*log(0,117647059))+(0,117647059*log(0,117647059))+(0,117647059*log(0,117647059)))

countCol6 = (-1/log(7)) * ((0,208333333*log(0,208333333))+(0,166666667*log(0,166666667))+(0,166666667*log(0,166666667))+(0,083333333*log(0,083333333))+(0,166666667*log(0,166666667))+(0,166666667*log(0,166666667))+(0,041666667*log(0,041666667)))

4
  • Check that $probabilitas[$i] contains a float value. I can't find where you define $probabilitas Commented Jan 5, 2019 at 6:20
  • probabilitas is an array that I have given above Commented Jan 5, 2019 at 6:27
  • Ah okay, didn't find the assignment. log($probabilitas[$i]) -> the parameter will always contain an array, because $probabilitas is two-dimensional. Commented Jan 5, 2019 at 6:39
  • @digijay im so confuse about what you say hhe Commented Jan 5, 2019 at 13:06

1 Answer 1

1

You are passing $probabilitas[$i] as a parameter to log(), but because of $probabilitas[$i] itself contains an array (the second dimension) you get the error message log() expects parameter 1 to be float, array given.

Looking at your function I suppose that you are missing the second iterator, so it should be like this:

$nEntropy[$j][$i] = 
    ((-1/log(7)) * 
        (($probabilitas[$j][$i]*log($probabilitas[$j][$i])) +
        ...
        ($probabilitas[$j][$i]*log($probabilitas[$j][$i])));

I hope this makes it clear. Also your code is missing a bracket, I've added it. Stackoverflow is not a debbugging service though ;-)

Sign up to request clarification or add additional context in comments.

2 Comments

yes this work for cleared error but the results is not like what i want
If it's not due to a mathematical mistake then it would be a new question.

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.