com.mathworks.toolbox.javabuilder.MWArray Class
Namespace: com.mathworks.toolbox.javabuilder
Java class to serve as base for all MATLAB array types with native array handling
Description
Declaration
public abstract class MWArray extends java.lang.Object implements java.lang.Cloneable, java.lang.Comparable, java.io.Serializable, Disposable
The 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
Implemented Interfaces: Disposable
,
java.io.Serializable
, java.lang.Cloneable
,
java.lang.Comparable
Direct Known Subclasses: MWCellArray
,
MWCharArray
, MWJavaObjectRef
,
MWLogicalArray
, MWMatrixRef
,
MWNumericArray
, MWStringArray
,
MWStructArray
Class Attributes: ,
Creation
Constructors
public MWArray()
Properties
Public Properties
EMPTY_ARRAY
— Represent an empty array
public static final MWArray
A convenient, efficient, and consistent way to represent an empty array as follows:
public static final MWArray EMPTY_ARRAY
Methods
Public Methods
applyVisitor |
This
method is abstract and returns a type specified by the type parameter <T>.
It takes an instance of Parameters:
Returns:
|
classID |
Returns the MATLAB type of this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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()); Class of A is int32 Returns: MWClassID of this array |
clone |
This method creates and returns a deep copy of the MWArray object. Because clone allocates a new array, any changes made to this new array are not reflected in the original. Example: Cloning an MWArray Object Object C = A.clone(); System.out.println("Clone of matrix A is:"); System.out.println(C.toString()); Clone of matrix A is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Overrides:
Returns: An MWArray instance representing a deep copy of the underlying MATLAB array. Throws:
|
columnIndex |
This method returns an array containing the column index of each element in the underlying MATLAB array. In the following example, the contents of the sparse MWArray (created by the 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 Returns: Array of indices. |
compareTo |
This method compares the MWArray object with the input object. It returns a negative integer, zero, or a positive integer if the MWArray object is less than, equal to, or greater than the specified object, respectively. 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"); Matrix S is equal to matrix A Specified by:
Parameters:
|
dispose |
This method destroys the native MATLAB array contained by the array object and frees the memory occupied by the array. Example: Constructing an MWArray Object Construct and then destroy an MWArray object as follows: MWArray A = new MWArray(); A.dispose(); Specified
by:
|
disposeArray |
This
method destroys any native MATLAB arrays contained in the input object and frees
the memory occupied by them. This is a static method of the class and thus does
not need to be invoked in reference to an instance of the class. If the input
object implements the Example: Constructing an MWNumericArray Object MWArray[] MArr = new MWArray[10]; for (int i = 0; i < 10; i++) MArr[i] = new MWNumericArray(); MWArray.disposeArray(MArr); Parameters:
|
equals |
This method indicates the MWArray object is equal to the input object. The equals method of the MWArray class overrides the equals method of class Object. Example: Comparing MWArrays with equals Object S = A.sharedCopy(); if (A.equals(S)) System.out.println("Matrix S is equal to matrix A"); Matrix S is equal to matrix A Overrides:
Parameters:
|
get |
Returns
the element at the specified 1-based offset in this array. Offers better
performance than 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]); } B: Cell data C(1,3) is 15 Parameters:
Returns: Object containing the requested element. Throws:
|
get |
Returns the element at the specified 1-based index-array in this array. Parameters:
Returns: Object containing the requested element. Throws:
|
getData |
Returns a 1-D array containing a copy of the data in the underlying MATLAB array. The elements of the returned array are converted according to default conversion rules. If the underlying MATLAB array is a complex numeric type, 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(); Data read from matrix A: 1 7 13 2 8 14 3 9 15 4 10 16 5 11 17 6 12 18 Returns: A 1-D array of elements stored in column-wise
order. The length of the returned array equals
|
getDimensions |
Returns an array containing the size of each dimension of this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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]); Dimensions of A are 3 x 6 Returns: array containing size of each dimension |
hashCode |
This method returns a hash code value for the MWArray object. The hashCode method of the MWArray class overrides the hashCode method of class Object. 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()); Hash code for matrix A is 45668 Overrides: hashCode in class java.lang.Object |
isEmpty |
This method returns true if the array object contains no elements, and false otherwise. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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 if (A.isEmpty()) System.out.println("Matrix A is empty"); else System.out.println("A = " + A.toString()); A= 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Returns:
|
isSparse |
This method returns true if the MWArray object is sparse, and false otherwise. The contents of the sparse MWArray, created by the 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"); Matrix A is sparse Returns:
|
maximumNonZeros |
This method returns the allocated capacity of a sparse array. If the underlying array is nonsparse, this method returns the same value as numberOfElements. The contents of the sparse MWArray, created by the 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 |
numberOfDimensions |
Returns the number of dimensions of this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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 elements in array object A: System.out.println("Matrix A has " + A.numberOfDimensions() + " dimensions"); When run, the example displays this output: Matrix A has 2 dimensions Returns: number of dimensions |
numberOfElements |
Returns the total number of elements in this array. This example uses a 3-by-6 MWNumericArray object A, as constructed by this Java code: 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 Returns: number of elements |
numberOfNonZeros |
This method returns the number of nonzero elements in a sparse array. If the underlying array is nonsparse, this method returns the same value as numberOfElements. The contents of the sparse MWArray, created by the 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: When run, the example displays this output: System.out.println("The number of nonzeros for matrix A is " + A.numberOfNonZeros()); The number of nonzeros for matrix A is 5 Returns: Current number of non-zero elements in a sparse array. |
rowIndex |
This method returns an array containing the row index of each element in the underlying MATLAB array. In the following example, the contents of the sparse MWArray (created by the 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(); Row indices are: 2 1 2 1 2 Returns: Array of indices. |
set |
Replaces
the element at the specified 1-based offset in this array with the specified
element. Offers better performance than 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()); Data read from A(2,4) is 555 Parameters:
Throws:
|
set |
Replaces the element at the specified 1-based index-array in this array with the specified element. Parameters:
Throws:
|
setData |
|
sharedCopy |
This method creates and returns a shared copy of the array. The shared copy points to the underlying original MATLAB array. Any changes made to the copy are reflected in the original. 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()); Shared copy of matrix A is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Returns: An MWArray instance representing a shared copy of the underlying MATLAB array. |
toArray | public abstract java.lang.Object[] toArray() Returns an array containing a copy of the data in the underlying MATLAB array. The returned array has the same dimensionality as the underlying MATLAB array. The elements of the returned array are converted according to default conversion rules. If the underlying MATLAB array is a complex numeric type, 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(); } Matrix A is: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Returns: An array with the same dimensionality of the MATLAB array. |
toString |
This method returns a string representation of the array. The toString method of the MWArray class overrides the toString method of class Object. 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 Overrides:
|
Inherited Methods
Methods inherited from class java.lang.Object
.
finalize |
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |
getClass |
Returns the runtime class of this Object. |
notify |
|
notifyAll |
Wakes up all threads that are waiting on this object's monitor. |
wait |
Causes
the current thread to wait until another thread invokes the
|
Examples
Hello World
More About
Inheritance Hierarchy
java.lang.Object
com.mathworks.javabuilder.MWArray
Version History
Introduced in R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)