public abstract class MWArray extends java.lang.Object implements java.lang.Cloneable, java.lang.Comparable, java.io.Serializable, Disposable
MWArray
class is the base class for all MATLAB array types.
This class stores a handle to a native MATLAB array and provides methods
for querying the array's properties and accessing the array's data.Modifier and Type | Field and Description |
---|---|
static MWArray |
EMPTY_ARRAY |
Constructor and Description |
---|
MWArray() |
Modifier and Type | Method and Description |
---|---|
abstract <T> T |
applyVisitor(AbstractMWArrayVisitor<T> v) |
abstract MWClassID |
classID()
Returns the MATLAB type of this array.
|
java.lang.Object |
clone()
This method creates and returns a deep copy of the MWArray object.
|
abstract int[] |
columnIndex()
This method returns an array containing the column index of
each element in the underlying MATLAB array.
|
abstract int |
compareTo(java.lang.Object obj)
This method compares the MWArray object with the input object.
|
abstract void |
dispose()
This method destroys the native MATLAB array contained by the
array object and frees the memory occupied by the array.
|
static void |
disposeArray(java.lang.Object obj)
This method destroys any native MATLAB arrays contained
in the input object and frees the memory occupied by them.
|
abstract boolean |
equals(java.lang.Object obj)
This method indicates the MWArray object is equal to the input
object.
|
abstract java.lang.Object |
get(int index)
Returns the element at the specified 1-based offset in this array.
|
abstract java.lang.Object |
get(int[] index)
Returns the element at the specified 1-based index-array in this array.
|
abstract java.lang.Object |
getData()
Returns a 1-D array containing a copy of the data in the underlying MATLAB array.
|
abstract int[] |
getDimensions()
Returns an array containing the size of each dimension of this array.
|
abstract int |
hashCode()
This method returns a hash code value for the MWArray object.
|
abstract boolean |
isEmpty()
This method returns true if the array object contains no elements,
and false otherwise.
|
abstract boolean |
isSparse()
This method returns true if the MWArray object is sparse,
and false otherwise.
|
abstract int |
maximumNonZeros()
This method returns the allocated capacity of a
sparse array.
|
abstract int |
numberOfDimensions()
Returns the number of dimensions of this array.
|
abstract int |
numberOfElements()
Returns the total number of elements in this array.
|
abstract int |
numberOfNonZeros()
This method returns the number of nonzero elements
in a sparse array.
|
abstract int[] |
rowIndex()
This method returns an array containing the row index of each
element in the underlying MATLAB array.
|
abstract void |
set(int[] index,
java.lang.Object element)
Replaces the element at the specified 1-based index-array in this array with the specified element.
|
abstract void |
set(int index,
java.lang.Object element)
Replaces the element at the specified 1-based offset in this array with the specified element.
|
abstract void |
setData(java.lang.Object data) |
abstract java.lang.Object |
sharedCopy()
This method creates and returns a shared copy of the array.
|
abstract java.lang.Object[] |
toArray()
Returns an array containing a copy of the data in the underlying MATLAB array.
|
abstract java.lang.String |
toString()
This method returns a string representation of the array.
|
public static final MWArray EMPTY_ARRAY
public java.lang.Object clone() throws java.lang.CloneNotSupportedException
Example: Cloning an MWArray Object
Create a clone of MWArray object A:Object C = A.clone(); System.out.println("Clone of matrix A is:"); System.out.println(C.toString());When run, the example displays this output:
Clone of matrix A is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
clone
in class java.lang.Object
MWArray
instance representing a deep copy of the
underlying MATLAB array.java.lang.CloneNotSupportedException
- - The object's class does not implement the Cloneable interface.public abstract java.lang.Object sharedCopy()
Example: Making a Shared Copy of an MWArray
Create a shared copy of MWArray object A:Object S = A.sharedCopy(); System.out.println("Shared copy of matrix A is:"); System.out.println(S.toString());When run, the example displays this output:
Shared copy of matrix A is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
MWArray
instance representing a shared copy of the
underlying MATLAB array.public abstract MWClassID classID()
int[][] Adata = {{ 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}}; MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Getting the Class ID of an MWArray
Return the class ID for an MWNumericArray object created previously as follows:System.out.println("Class of A is " + A.classID());When run, the example displays this output:
Class of A is int32
MWClassID
of this arraypublic abstract int numberOfDimensions()
int[][] Adata = {{ 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}}; MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Getting the Number of Dimensions of an MWArray
Display the number of dimensions for array object A:System.out.println("Matrix A has " + A.numberOfDimensions() + " dimensions");When run, the example displays this output:
Matrix A has 2 dimensions
public abstract int[] getDimensions()
int[][] Adata = {{ 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}}; MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Getting Array Dimensions of an MWArray
int[] dimA = A.getDimensions(); System.out.println("Dimensions of A are " + dimA[0] + " x " + dimA[1]);When run, the example displays this output:
Dimensions of A are 3 x 6
public abstract boolean isEmpty()
int[][] Adata = {{ 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}}; MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Testing for an Empty MWArray
Display a message if array object A is an empty array. Otherwise, display the contents of A:if (A.isEmpty()) System.out.println("Matrix A is empty"); else System.out.println("A = " + A.toString());When run, the example displays the contents of A:
A= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public abstract boolean isSparse()
newSparse
method, used in the following example are:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90
Example: Testing an MWArray for Sparseness
Test the MWArray object A created previously for sparseness:if (A.isSparse()) System.out.println("Matrix A is sparse");When run, the example displays this output:
Matrix A is sparse
public abstract boolean equals(java.lang.Object obj)
Example: Comparing MWArrays with equals
Create a shared copy of the MWArray object and then compare it to the original object. A return value of true indicates that the two objects are equal:Object S = A.sharedCopy(); if (A.equals(S)) System.out.println("Matrix S is equal to matrix A");When run, the example displays this output:
Matrix S is equal to matrix A
equals
in class java.lang.Object
obj
- Object Array to compare with this MWArray objectpublic abstract int compareTo(java.lang.Object obj)
Example: Comparing MWArrays with compareTo
Create a shared copy of the MWArray object and then compare it to the original object. A return value of zero indicates that the two objects are equal:Object S = A.sharedCopy(); if (A.compareTo(S) == 0) System.out.println("Matrix S is equal to matrix A");When run, the example displays this output:
Matrix S is equal to matrix A
compareTo
in interface java.lang.Comparable
obj
- Object Array to compare with this MWArray objectpublic abstract int hashCode()
Example: Getting an MWArray Hash Code
Obtain the hash code for MWArray object A:System.out.println("Hash code for matrix A is " + A.hashCode());When run, the example displays this output:
Hash code for matrix A is 45668
hashCode
in class java.lang.Object
public abstract java.lang.String toString()
Example: Converting an MWArray to a String
Display the contents of MWArray object A:System.out.println("A = " + A.toString());When run, the example displays the contents of A:
A= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
toString
in class java.lang.Object
public abstract int numberOfElements()
int[][] Adata = {{ 1, 2, 3, 4, 5, 6}, { 7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}}; MWNumericArray A = new MWNumericArray(Adata, MWClassID.INT32);
Example: Getting the Number of MWArray Elements
Display the number of elements in array object A:System.out.println("Matrix A has " + A.numberOfElements() + " elements");When run, the example displays this output:
Matrix A has 18 elements
public abstract int numberOfNonZeros()
newSparse
method, used in the following example are:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90
Example: Getting the Number of Nonzeros in an MWArray
Display the number of nonzero values in this array:System.out.println("The number of nonzeros for matrix A is " + A.numberOfNonZeros());When run, the example displays this output:
The number of nonzeros for matrix A is 5
public abstract int maximumNonZeros()
newSparse
method, used in the following example are:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90
Example: Getting the Maximum Number of Nonzeros in an MWArray
Display the maximum number of nonzeros for this array:System.out.println("Maximum number of nonzeros for matrix A is " + A.maximumNonZeros());When run, the example displays this output:
Maximum number of nonzeros for matrix A is 10
public abstract void dispose()
Example: Constructing an MWArray Object
Construct and then destroy an MWArray object as follows:MWArray A = new MWArray(); A.dispose();
dispose
in interface Disposable
public abstract java.lang.Object get(int index)
public Object get(int[] index)
.Example: Getting an MWArray Value with get
int[] cdims = {1, 3}; MWArray C = new MWArray(cdims); Integer val = new Integer(15); int[] index2 = {1, 3}; C.set(index2, val); Object x = C.get(index2); if (x instanceof int[][]) { int[][] y = (int[][])x; System.out.println("B: Cell data C(1,3) is " + y[0][0]); }When run, the example displays this output:
B: Cell data C(1,3) is 15
index
- The index of the requested element. Valid range: 1 <= index <= N, where N = total number of elements in the array.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public abstract java.lang.Object get(int[] index)
index
- Array of indices specifying the location of the requested element.
The length of the index array must be exactly the number of dimensions of this array.
Each element of the index array has the valid range: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public abstract void set(int index, java.lang.Object element)
public void set(int[] index, Object element)
.
Example: Setting an MWArray Value
Modify the data in element (2, 4) of MWArray object A:int[] index = {2, 4}; A.set(index, 555); Object d_out = A.get(index); System.out.println("Data read from A(2,4) is " + d_out.toString());When run, the example displays this output:
Data read from A(2,4) is 555
index
- The index of the element to replace. Valid range: 1 <= index <= N, where N = total number of elements in the array.element
- New element to replace at index.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public abstract void set(int[] index, java.lang.Object element)
index
- Array of indices specifying the location of the element to replace.
The length of the index array must be exactly the number of dimensions of this array.
Each element of the index array has the valid range: 1 <= index[i] <= N[i], where N[i] = the size of the ith dimension.element
- New element to replace at index.java.lang.IndexOutOfBoundsException
- An invalid index has been supplied.public abstract java.lang.Object[] toArray()
toArray
returns the real part. If the underlying array is sparse, a full representation of
the array is returned. Care should be taken when calling toArray
on a
sparse array with large row and column dimensions, as this action may exhaust system
memory. If the underlying array is a cell or struct array, toArray
is
recursively called on each cell.
Example: Getting an MWArray with toArray
Create and display a copy of MWArray object A:int[][] x = (int[][]) A.toArray(); int[] dimA = A.getDimensions(); System.out.println("Matrix A is:"); for (int i = 0; i < dimA[0]; i++) { for (int j = 0; j < dimA[1]; j++) System.out.print(" " + x[i][j]); System.out.println(); }When run, the example displays this output:
Matrix A is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public abstract java.lang.Object getData()
getData
returns
the real part. If the underlying array is a cell or struct array, toArray
is
recursively called on each cell.
Example: Getting an MWArray Value with getData
Get the data from MWArray object A, casting the type from Object to int:System.out.println("Data read from matrix A:"); int[] x = (int[]) A.getData(); for (int i = 0; i < x.length; i++) System.out.print(" " + x[i]); System.out.println();When run, the example displays this output:
Data read from matrix A: 1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12 18
numberOfElements()
for a non-sparse array, and numberOfNonZeros()
for a sparse array.public abstract void setData(java.lang.Object data)
public abstract int[] rowIndex()
newSparse
method) are:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90
Example: Getting the Row Indices of a Sparse MWArray
Get the row indices of the elements of the sparse array:System.out.print("Row indices are: "); int[] rowidx = A.rowIndex(); for (int i = 0; i < 5; i++) System.out.print(rowidx[i] + " "); System.out.println();When run, the example displays this output:
Row indices are: 2 1 2 1 2
public abstract int[] columnIndex()
newSparse
method) are:
(2,1) 50 (1,2) 10 (2,2) 60 (1,5) 40 (2,5) 90
Example: Getting the Column Indices of a Sparse MWArray
Get the column indices of the elements of the sparse array:System.out.print("Column indices are: "); int[] colidx = A.columnIndex(); for (int i = 0; i < 5; i++) System.out.print(colidx[i] + " "); System.out.println();When run, the example displays this output:
Column indices are: 1 2 2 5 5
public abstract <T> T applyVisitor(AbstractMWArrayVisitor<T> v)
public static void disposeArray(java.lang.Object obj)
Disposable
interface, the object
is freed by calling its Disposable.dispose()
method. If the input object represents
an array of Disposable
instances, each object in the array is disposed.
If the input object represents an array of java.lang.Object, or a multi-dimensional
array, the array is recursively processed to free each Disposable
instance contained
in the array.
Example: Constructing an MWNumericArray Object
Construct and then destroy an array of numeric objects as follows:MWArray[] MArr = new MWArray[10]; for (int i = 0; i < 10; i++) MArr[i] = new MWNumericArray(); MWArray.disposeArray(MArr);
obj
- Object to process.© 1994-2017 The MathWorks, Inc. Patents Trademarks