Poensvah
Poensvah

Reputation: 15

What is the java representation of size in matlab

This is the size function defined in Matlab as seen in the docs:

m = size(X,dim) returns the size of the dimension of X specified by scalar dim.

1) Given that X is an single array in java, how would I replicate this method using Java.

Upvotes: 0

Views: 128

Answers (3)

reverse_engineer
reverse_engineer

Reputation: 4269

Matlab just stores matrix and n-dimensional data linearly in an array. Each dimension's size is saved along with the array, so Matlab knows which element to return when you index not linearly (e.g. A(3,5) for a 5x5 matrix, Matlab knows it should return element A(23) which is 3+(5-1)*5).

So in java if an array has size N1xN2x...xNN, and your looking for element: (X1,X2,...,XN), you should find element with position: X1+(X2-1)*N1+(X3-1)*N1*N2+ ... +(XN-1)*NN-1*..*N1 in the array...

Upvotes: 1

JSQuareD
JSQuareD

Reputation: 4776

An array in Java only has one dimension: the number of elements. You can access this simply by using the property Array.length. For more complex containers (such as ArrayList) you can often use the member-function size().

int[] array = new int[5];
int arrayLength = array.length; //arrayLength == 5

ArrayList<int> arrayList = new ArrayList();
arrayList.add(1);
arrayList.add(2);
int arrayListLength = arrayLis.size(); //arrayListLength == 2

Upvotes: 0

PermGenError
PermGenError

Reputation: 46408

If you want just the size of the array(any dimention), you have an attribute Array.length.

From JLS:

The public final field length, which contains the number of components of the array. length may be positive or zero.

Eg:

int[] arr = new int[10];
int length = arr.length; //would obtain 10

Upvotes: 0

Related Questions