Académique Documents
Professionnel Documents
Culture Documents
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
“TEORÍA DE MATRICES”
GRUPO:
Abanto Cotrina, Helber
Chávez Coronado, Judith
Cubas Walter, Mabely
Malimba Bacón, Luzmila
CICLO: IX
EJEMPLOS:
' Change the size of an existing array to 16 elements and retain the current
values.
ReDim Preserve numbers(15)
' Redefine the size of an existing array and reset the values.
ReDim numbers(15)
1
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
En el siguiente ejemplo contiene el código de Visual Basic que crea y utiliza la matriz:
Module SimpleArray
Public Sub Main()
' Declare an array with 7 elements.
Dim students(6) As Integer
2
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
3
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Si tiene una matriz existente, puede volver a definir su tamaño mediante el uso de
la Redim instrucción. Puede especificar que el Redim instrucción mantener los valores
que se encuentran en la matriz, o puede especificar qué cree una matriz vacía. El
ejemplo siguiente muestra los diferentes usos de la instrucción Redim para modificar
el tamaño de una matriz existente.
' Assign a new array size and retain the current values.
ReDim Preserve cargoWeights(20)
' Assign a new array size and retain only the first five values.
ReDim Preserve cargoWeights(4)
' Assign a new array size and discard all current element values.
ReDim cargoWeights(15)
Module Example
Public Sub Main()
' Create a 10-element integer array.
Dim numbers(9) As Integer
Dim value As Integer = 2
4
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Next
Tamaño de la matriz
El tamaño de una matriz es el producto de las longitudes de todas sus
dimensiones. Representa el número total de elementos contenidos actualmente en la
matriz. Por ejemplo, en el ejemplo siguiente se declara una matriz unidimensional de
2 con cuatro elementos en cada dimensión. Como se muestra en el resultado del
ejemplo, el tamaño de la matriz es 16 (o (3 + 1) * (3 + 1).
Module Example
Public Sub Main()
Dim arr(3, 3) As Integer
Console.WriteLine(arr.Length)
End Sub
End Module
' The example displays the following output:
' 16
Nota
Esta discusión de tamaño de la matriz no se aplica a las matrices escalonadas.
5
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Module Example
Public Sub Main()
Dim arr(99) As Integer
Console.WriteLine(arr.Length)
Redim arr(50)
Console.WriteLine(arr.Length)
End Sub
End Module
' The example displays the following output:
' 100
' 51
Matriz unidimensional
Matriz bidimensional
Matriz Tridimensional
6
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
El tipo de matriz
Cada matriz tiene un tipo de datos, que difiere del tipo de datos de sus elementos. No
existe ningún tipo de datos para todas las matrices. En su lugar, el tipo de datos de
una matriz lo determina el número de dimensiones, o rango, de la matriz y el tipo de
datos de los elementos de la matriz. Dos variables de matriz son de los mismos datos
escriba solo cuando tienen el mismo rango y sus elementos tienen los mismos datos
de tipo. Las longitudes de las dimensiones de una matriz no influyen en el tipo de
datos de matriz.
Cada matriz hereda la clase System.Array y puede declarar una variable del tipo Array ,
pero no puede crear una matriz del tipo Array . Por ejemplo, aunque el código
siguiente declara la arr variable para que sea de tipo Array y llama a
la Array.CreateInstance demuestra el método para crear una instancia de la matriz, el
tipo de la matriz como Object [].
Module Example
Public Sub Main()
Dim arr As Array = Array.CreateInstance(GetType(Object), 19)
Console.WriteLine(arr.Length)
Console.WriteLine(arr.GetType().Name)
End Sub
End Module
' The example displays the following output:
' 19
' Object[]
7
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Module Example
Public Sub Main()
Dim bytes(9,9) As Byte
Console.WriteLine($"Type of {nameof(bytes)} array: {bytes.GetType().Name}")
Console.WriteLine($"Base class of {nameof(bytes)}:
{bytes.GetType().BaseType.Name}")
Console.WriteLine()
Console.WriteLine($"Type of {nameof(bytes)} array: {TypeName(bytes)}")
End Sub
End Module
' The example displays the following output:
' Type of bytes array: Byte[,]
' Base class of bytes: Array
'
' Type of bytes array: Byte(,)
Se declara una matriz de longitud cero con una dimensión de -1. Por ejemplo:
Puede que tenga que crear una matriz de longitud cero en las circunstancias
siguientes:
8
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Una vez que haya determinado el índice o índices en el que se debe dividir la matriz,
a continuación, puede crear las matrices individuales mediante una llamada a
la Array.Copy método.
Module Example
Public Sub Main()
' Create an array of 100 elements.
Dim arr(99) As Integer
' Populate the array.
Dim rnd As new Random()
For ctr = 0 To arr.GetUpperBound(0)
arr(ctr) = rnd.Next()
Next
9
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Module Example
Public Sub Main()
Dim rnd As New Random()
10
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Combinación de matrices
También puede combinar un número de matrices en una sola matriz mayor. Para
ello, utiliza también el Array.Copy método.
Antes de copiar los elementos de cada matriz en la nueva matriz, primero debe
asegurarse de que se ha inicializado la matriz para que sea suficientemente grande
como para acomodar la nueva matriz. Hay dos maneras de hacerlo:
En el ejemplo siguiente se usa el segundo enfoque para agregar cuatro matrices con
diez elementos a una sola matriz.
Imports System.Collections.Generic
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks As New List(Of Task(Of Integer()))
' Generate four arrays.
For ctr = 0 To 3
Dim value = ctr
tasks.Add(Task.Run(Function()
Dim arr(9) As Integer
For ndx = 0 To arr.GetUpperBound(0)
arr(ndx) = value
Next
Return arr
End Function))
Next
Task.WaitAll(tasks.ToArray())
' Compute the number of elements in all arrays.
Dim elements = 0
For Each task In tasks
elements += task.Result.Length
Next
Dim newArray(elements - 1) As Integer
11
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Dim index = 0
For Each task In tasks
Dim n = task.Result.Length
Array.Copy(task.Result, 0, newArray, index, n)
index += n
Next
Console.WriteLine($"The new array has {newArray.Length} elements.")
End Sub
End Module
' The example displays the following output:
' The new array has 40 elements.
Puesto que en este caso, las matrices de origen son todos los pequeñas, podemos
expandir dinámicamente también la matriz cuando agreguemos los elementos de
cada matriz nuevo a él. En el siguiente ejemplo se realiza esto.
Imports System.Collections.Generic
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim tasks As New List(Of Task(Of Integer()))
' Generate four arrays.
For ctr = 0 To 3
Dim value = ctr
tasks.Add(Task.Run(Function()
Dim arr(9) As Integer
For ndx = 0 To arr.GetUpperBound(0)
arr(ndx) = value
Next
Return arr
End Function))
Next
Task.WaitAll(tasks.ToArray())
' Dimension the target array and copy each element of each source array to
it.
Dim newArray() As Integer = {}
' Define the next position to copy to in newArray.
Dim index = 0
For Each task In tasks
Dim n = Task.Result.Length
ReDim Preserve newArray(newArray.GetUpperBound(0) + n)
Array.Copy(task.Result, 0, newArray, index, n)
12
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
index += n
Next
Console.WriteLine($"The new array has {newArray.Length} elements.")
End Sub
End Module
' The example displays the following output:
' The new array has 40 elements.
1. SUMA Y RESTA
13
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Radio botton1
DataGridView1.columns.Clear ( )
DataGridView2.columns.Clear ( )
DataGridView3.columns.Clear ( )
For i=0 to 1
DataGridView1.columns.Add (i,” ”)
DataGridView2.columns.Add (i,” ”)
DataGridView1.Rows.Add ( )
DataGridView2.Rows.Add ( )
Next
Operación= “2X2”
End Sub
DataGridView1.columns.Clear ( )
DataGridView2.columns.Clear ( )
DataGridView3.columns.Clear ( )
For i=0 to 2
DataGridView1.columns.Add (i,” ”)
DataGridView2.columns.Add (i,” ”)
DataGridView1.Rows.Add ( )
DataGridView2.Rows.Add ( )
Next
Operación = “3X3”
End Sub
Vacios = True
14
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
For i =0 to 1
For j = 0 to 1
Vacios = False
End If
Next
Next
Else
For i =0 to 2
For j = 0 to 2
Vacios = False
End If
Next
Next
End If
End If
End Sub
Vacio ( )
DataGridView1.columns.Clear ( )
For i = 0 to 1
DataGridView3.columns.Add(i,””)
DataGridView3.Rows.Add ( )
Netx
For i = 0 to 1
For j = 0 to 1
DataGridView3.Item(i,j).Value = Val1(DataGridView1.Item(i,j).VAlue) +
(DataGridView2.Item(i,j).Value)
15
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Operacion.text = “+”
Next
Next
End If
End Sub
Vacio ( )
DataGridView1.columns.Clear ( )
For i = 0 to 1
DataGridView3.columns.Add(i,””)
DataGridView3.Rows.Add ( )
Netx
For i = 0 to 1
For j = 0 to 1
DataGridView3.Item(i,j).Value = Val1(DataGridView1.Item(i,j).VAlue) -
(DataGridView2.Item(i,j).Value)
Next
Next
Operacion.text = “-”
For i = 0 to 2
DataGridView3.columns.Add(i,””)
DataGridView3.Rows.Add ( )
Next
For i = 0 to 2
For j = 0 to 2
DataGridView3.Item(i,j).Value = Val1(DataGridView1.Item(i,j).VAlue) -
(DataGridView2.Item(i,j).Value)
Next
Next
Operacion.text = “-”
End If
End Sub
16
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
End If
End Sub
End Clas
2. MULTIPLICACION
fila = Val(TextBox1.Text)
columna = Val(TextBox2.Text)
DataGridView2.RowCount = fila
DataGridView2.ColumnaCount = columna
For i = 0 To columna - 1
DataGridVie2.Columna(i).Width = 50
Next
DataGridView2.RowCount = fila
DataGridVie3.ColumnaCount = columna
For i = 0 To columna - 1
DataGridVie2.Columna(i).Width = 50
Next
Else
MsgBox("Solo se puede sumar matrices cuadradas", 0 + 16, "Error")
TextBox1.Text = " "
TextBox2.Text = " "
TextBox1.Focus()
End If
End Sub
17
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
Dim i, j, h As Byte
Dim valor As Integer
Dim fila As Byte, columna As Byte
columna = Val(TextBox1.Text)
fila = Val(TextBox2.Text)
For h = 0 To fila - 1
For i = 0 To columna - 1
valor = 0
For j = 0 To columna - 1
valor = valor + Val(DataGridView1.Rows(h).Cells(j).Value) *
ValVal(DataGridView2.Rows(j).Cells(i).Value)
Next
DataGridView3.Rows(h).Cells(i).Value = valor
Next
Next
End Sub
columna = Val(TextBox1.Text)
fila = Val(TextBox2.Text)
For i = 0 To fila - 1
For j = 0 To columna - 1
DataGridView1.Rows(i).Cells(j).Value = " "
DataGridView2.Rows(i).Cells(j).Value = " "
DataGridView3.Rows(i).Cells(j).Value = " "
Next
Next
End Sub
End Class
18
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
19
UNIVERSIDAD NACIONAL DE CAJAMARCA
FACULTAD DE EDUCACIÓN
ESPECIALIDAD DE MATEMÁTICA E INFORMÁTICA
20