基于行和列的多维数组计算

时间:2019-01-05 06:06:04

标签: php multidimensional-array

我在多维数组方面遇到麻烦。我想根据行和列的计算结果

我有这样的数组概率

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
    ) 
)

如果在浏览器上运行将是这样 enter image description here

我想得到这样的最终结果

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

但是我有这样的错误

  

消息:log()期望参数1为浮点数,给定数组

我的代码有什么问题?有人可以帮我吗?

这是我的代码

$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])));
            }
    }

要计算它,我使用此公式。

  

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)))

1 个答案:

答案 0 :(得分:1)

您正在将$probabilitas[$i]作为参数传递给log(),但是由于$probabilitas[$i]本身包含一个数组(第二维),您会收到错误消息log() expects parameter 1 to be float, array given。 / p>

查看您的函数,我想您缺少第二个迭代器,所以应该像这样:

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

我希望这一点可以弄清楚。另外,您的代码缺少括号,我已经添加了它。 Stackoverflow并不是debbugging服务;;-)