本文介绍了怎么算频率二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨即时试图计算一个二维数组的频率。我试图在例如以某种方式显示频率,如果一个表像:

Hi im trying to count the frequency of a 2d array . i am trying to display the frequency in a certain way for example if a table is like :

0:1 2 0

1:2 0 1

2:1 0 2

我希望能够像计数频率:

i want to be able to count the frequency like :

0:0 2 1

1:2 0 1

2:1 1 1

这样的方式,该表将应该是多少次0出现在第一列和多少次1具有在第一列中的出现,然后依此类推。

so the way that the table would should is how many times 0 has appeared in the first column and how many times 1 has a appears in the first column and then so on.

在code我到目前为止这是

the code i have so far for this is

    for (int t = 0; t<s;t++)
    {
        int count= 0 ;
        for (int p = 0; p<s; p++)
        {
            if(table[p][t] ==p )
            {
                count++

            }
            else if(t+1 != s)
                continue;
            else
                table[p][t] = count;
                count = 0;
        }
    }

感谢您的帮助。

推荐答案

如果您发布的例子是什么数组总是会像(即唯一可能的值是0,1和2),那么你可以做事情如下: -

If the example you posted is what the array is always going to be like (i.e. the only values possible are 0, 1, and 2) then you can do something as follows -

int[][] counts = new int[3][3]; //this inits all the elements to 0
//iterate through all the elements in your source array
for(int row=0; row<source.length; row++) {
    for(int col=0; col<source[row].length; col++) {
        //just increment the appropriate element of the counts array here
        counts[col][source[row][col]]++;
    }
}

这篇关于怎么算频率二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:31
查看更多