Creation And Use Of Matrixes
Enviado por marianalozano123 • 8 de Noviembre de 2012 • 1.503 Palabras (7 Páginas) • 540 Visitas
A matrix is an object in which you can store a set of data of the same type.
Matrixes are also known as arrays.
A matrix or array is defined based of the number of dimensions.
A one dimension matrix is also called a uni-dimensional matrix, and you can graphically represent it as follows:
A two dimensional matrix is also called a bidimensional matrix and you can represent it graphically as follows:
A matrix with tree dimensions is also called a tridimensional matrix and it is represented as a cube:
You can also create matrixes with more dimensions, even though they have no graphic representation.
A uni-dimensional matrix can also be declared using the following syntax:
Data_type Matrixname[];
You can then add the following instruction:
Matrixname = new datatype[length];
Example:
int m[]; //Matrix with the name of “m” that stores integer values.
m=new int[16]; // length of the matrix is 16.
Once you have declared the matrix you can assign the values to each of the “spaces”.
To identify each of the spaces you make use of a index that is an integer number that indicates the position of each space.
The index starts with the zero and increments one by one.
Example:
For the matrix m there are certain assigned spaces to store integer numbers. For example, if you wish to assign the number 728 in the space number 6 you must use the index in the position 5 (remember that the index starts with the position 0).
This way you can assign a value to any space inside the matrix.
The syntax to assign a value into a space in the matrix is as follows:
Matrixname[index_number] = value;
Example:
int m[];
m[5]=728 //Assigns the value of 728 to the position 5.
12.2 Define the matrix values by using the length attribute and a loop
Once you declare a unidimensional matrix you face the following challenge:
¿What is the max quantity of spaces assigned to our matrix?
The answer is: “inifinite”, there is really no limit to the matrix, unless you assign it with a size.
To specify the size or number of spaces the matrix will have you must specify the value in-between brackets.
Example:
Once you have specified the size of the matrix you can begin to assign values with the use of a loop.
Example:
Assigns the value of the index to the respective position of the matrix.
All the variables that make reference to an object of matrix type have an attribute that allows them to know the size of the matrix, this attribute is the length.
In the next example you can see how a matrix is declared and how you can fill the space with values that go from 0 to 9 by using a loop for:
public class matrix_uni{
public static void main (String [ ] args) {
int p[];
p= new int [10]; // the length is 10 but the index goes from 0 to 9
p.length //Returns the value in
...