Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中是否有MATLAB函数numel的对应项?_Java_Matlab - Fatal编程技术网

java中是否有MATLAB函数numel的对应项?

java中是否有MATLAB函数numel的对应项?,java,matlab,Java,Matlab,我正在尝试使用java语言(函数在Matlab中可用)。 java中是否有此函数的实现?matlab的numel函数 n=numel(A)返回数组A中元素数n 你可以让你的 int arraycount(int a[]) { int counter; for(int i=0;i<a.length;i++) { counter++; } return counter; } int数组计数(int a[] { 整数计数器; 对于

我正在尝试使用java语言(函数在Matlab中可用)。
java中是否有此函数的实现?

matlab的numel函数

n=numel(A)返回数组A中元素数n

你可以让你的

int arraycount(int a[])
    {
    int counter;
    for(int i=0;i<a.length;i++)
    {
    counter++;
    }
    return counter;
    }
int数组计数(int a[]
{
整数计数器;

对于(int i=0;i我不确定这是否是您希望
numel
的工作方式,但这里有几个版本:

version1-对于大小不规则的数组,如{1}、{2,3} 此方法将迭代数组中的所有元素,并对它们进行计数

public static int numel(Object array) {
    if (array == null)
        return 1;// I will count nulls as elements since new String[10] is
                    // initialized with nulls 
    int total = 1;
    if (array.getClass().isArray()) {
        total = 0;
        int length = java.lang.reflect.Array.getLength(array);
        for (int index = 0; index < length; index++) {
            total += numel(java.lang.reflect.Array.get(array, index));
        }
    }
    return total;
}

我设法解决了这个问题

我多么希望图像的大小,我做了一个小函数来解决我的问题

 public static int length(BufferedImage bi)
{
    int x;
    x=bi.getHeight()*bi.getWidth();
    return x;
}

我相信如果数组是3x3,这会得到3,甚至是3x3x3。我相信如果数组是3x3,甚至是3x3x3,这会得到3。作为旁注,如果你有一个M×N×Z矩阵或数组,numel会打印出MxNxZ。在matlab中,numel与打印出max(N,M,Z)的长度不同这可能很有趣:在Java中,多维数组的“行”可以有不同的大小,比如
int[]myArray={{{1,2},{3,4,5,6};
.How
numel(myArray)
应该对这类数据做出反应吗?我经常在上下文中遇到
numel
作为所有元素的迭代器。几乎总是可以用Java中的某种模式来替换它。想想迭代器、聚合器或映射器。这与您的问题有什么关系?人们怎么知道您正在尝试获取BuffereImage的大小?问题很有可能,我找不到最好的方式来解释我的问题。你的答案与你的问题无关。下次描述你试图实现的目标,然后展示你是如何尝试实现的。没有它,人们将无法帮助你。看看你的问题和答案
public static int regularNumel(Object array) {
    if (array == null)
        return 1;
    int total = 1;
    if (array.getClass().isArray()) {
        int length = java.lang.reflect.Array.getLength(array);
        if (length > 0) {
            Object row = java.lang.reflect.Array.get(array, 0);
            if (row == null || !row.getClass().isArray())
                return length;
            else //now we know that row is also array
                return length * regularNumel(row);
        } else
            return 0;
    }
    return total;
}
 public static int length(BufferedImage bi)
{
    int x;
    x=bi.getHeight()*bi.getWidth();
    return x;
}