Computer Science

Matrix Operations in C

Matrix operations in C involve performing various mathematical operations on matrices using the C programming language. These operations can include addition, subtraction, multiplication, and transposition of matrices. By using C, programmers can efficiently manipulate and process matrices to perform tasks such as solving systems of linear equations, implementing algorithms for image processing, and conducting scientific computations.

Written by Perlego with AI-assistance

4 Key excerpts on "Matrix Operations in C"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Fundamental Mass Transfer Concepts in Engineering Applications
    • Ismail Tosun(Author)
    • 2019(Publication Date)
    • CRC Press
      (Publisher)
    C Matrices C.1 Basic Matrix Operations A rectangular array of elements or functions is called a matrix. If the array has m rows and n columns, it is called a matrix of order (m, n) or an m × n matrix and is expressed in the form [ A ] = (A 1 1 A 1 2 A 1 3... A 1 n A 2 1 A 2 2 A 2 3... A 2 n ….. ….. ….. … ….. A m 1 A m 2 A m 3... A m n) (C.1) The numbers or. functions A ij are called the elements of a matrix. Equation (C.1) is also expressed as [ A ] = (A i j), (C.2) in which the subscripts i and j represent the row and the column of the matrix, respectively. A matrix having only one row is called a row matrix (or row vector) while a matrix having only one column is called a column matrix (or column vector). When the number of rows and the number of columns are the same, i.e., m = n, the matrix is called a square matrix or a matrix of order n (or m). The fundamental algebraic operations involving matrices can be briefly summarized as follows: Two matrices [A] = (A ij) and [B] = (B ij) of the same order are equal if and only if A ij = B ij. If [A] = (A ij) and [B] =. (B ij) have the same order, the sum of [A] and [B] is defined as [ A ] + [ B ] = (A i j + B i j) (C.3) Matrix addition satisfies the following algebraic rules: Commutative [ A ] + [ B ] = [ B ] + [ A ] Associative [ A ] + ([ B ] + [ C ]) = ([ A ] + [ B ]) + [ C ] If [A] = (A ij) and [B] = (B ij) have the same order, the difference between [A] and [B]. is defined as [ A ] - [ B ] = (A i j - B i j) (C.4) If [A] = (A ij) and λ is any number, the product of [A] by λ is defined as λ [ A ] = [ A ] λ = (λ A i j) (C.5) The product of two matrices [A] and [B], [A][B], is defined only if the number of columns in [A] is equal to the number of rows in [B]. For example, if [A] is of order (4,2) and [B] is of order (2,3), then the product [A] [B ]. is [ A ] [ B ] = (A 1 1 A 1 2 A 2 1 A 2 2 A 3 1 A 3 2 A 4 1 A 4 2) ⋅ (B 1 1 B 1 2 B 1 3 B 2 1 B 2 2 B 2 3) = (A 1 1 B 1 1 + A 1 2 B 2 1 A 1 1 B 1 2 + A 1 2 B 2 2[--=PLGO-SEP
  • Mathematical Economics
    • Arsen Melkumian(Author)
    • 2012(Publication Date)
    • Routledge
      (Publisher)
    6    Matrix algebra
    Matrices are essentially tables with numbers. As such, they are as frequent as the tables you create in applications like Microsoft Excel. Many business data are naturally organized as tables. For instance, columns may label various types of merchandise a firm has, while rows list various business metrics of the merchandise (metrics like cost, profit, labor per unit, etc.). However, unlike tables, which just store data, there are meaningful algebraic operations that we can apply to matrices. For instance, matrix multiplication naturally models such fundamental operations as totaling across certain parameters.
    6.1 Introduction
    A matrix is a rectangular array of elements. These elements may be numbers, parameters, variables or functions. A matrix that has m rows and n columns is said to have dimension m × n (read “m by n”). A matrix that has m rows and n columns sometimes is said to have order m × n. A matrix of dimension m × n can be written in three forms:
    We can also write Dim (A) = m × n to indicate that matrix A has a dimension of m × n.
    EXAMPLE 6.1 The array
    is called a 3 × 4 matrix.
    A matrix that has m rows and n columns is said to have a row dimension of m and a column dimension of n. The location of each element a
    ij
    in a matrix is given by two subscripts, i and j. For instance, the element a34 is located in the third row and fourth column.
    Matrix A is said to be a square matrix if Dim (A) = n × n. In other words, a matrix is square if the number of its rows is equal to the number of its columns.
    EXAMPLE 6.2 Matrices
    and
    are square matrices of dimension 4 × 4 and 3 × 3 respectively. Sometimes a square matrix of order n × n is referred to as a square matrix of order n.
    The main diagonal of a matrix consists of elements for which i equals j. For instance, the main diagonal of matrix A1 in Example 6.1 consists of elements 1, 21 and 11. The main diagonal of a square matrix is the diagonal that runs from the top left corner to the bottom right corner. Note that the main diagonal of matrix A3
  • Fundamentals of Electric Machines: A Primer with MATLAB
    • Warsame Hassan Ali, Matthew N. O. Sadiku, Samir Abood(Authors)
    • 2019(Publication Date)
    • CRC Press
      (Publisher)
    In addition to operating on mathematical functions, MATLAB easily allows one to work with vectors and matrices. A vector (or array) is a special matrix with one row or one column. For example:
    >> a = [1 −3 6 10 −8 11 14];
    is a row vector. Defining a matrix is similar to defining a vector. For example, a 3 × 3 matrix can be entered as:
    >> A = [1 2 3; 4 5 6; 7 8 9]
    or as:
    >> A = [1 2 3 4 5 6 7 8 9]
    In addition to the arithmetic operations that can be performed on a matrix, the operations in Table C.3 can be implemented.
    Using the operations in Table C.3 , we can manipulate matrices as follows.
    » B = A’ B =     1 4 7     2 5 8     3 6 9 » C = A + B
    TABLE C.3Matrix Operations
    Operation Remark
    A’ Finds the transpose of matrix A
    det(A) Evaluates the determinant of matrix A
    inv(A) Calculates the inverse of matrix A
    eig(A) Determines the eigenvalues of matrix A
    diag(A) Finds the diagonal elements of matrix A
    expm(A) Exponential of matrix A
      C =     2 6 10     6 10 14     10 14 18 » D = A^3 - B*C   D =     372 432 492     948 1131 1314     1524 1830 2136 » e= [1 2; 3 4]   e =     1 2     3 4 » f=det(e)   f =     -2 » g = inv(e)   g =     -2.0000 1.0000     1.5000 -0.5000 » H = eig(g)   H =     -2.6861     0.18611
    Note that not all matrices can be inverted. A matrix can be inverted if and only if its determinant is nonzero. Special matrices, variables, and constants are listed in Table C.4 . For example, type:
    >> eye(3)
    TABLE C.4Special Matrices, Variables, and Constants
    Matrix/Variable/Constant Remark
    eye Identity matrix
    ones An array of ones
    zeros An array of zeros
    i or j Imaginary unit or sqrt(–1)
    pi 3.142
    NaN Not a number
    inf Infinity
    eps A very small number, 2.2e–16
    rand Random element
    ans=     1 0 0     0 1 0     0 0 1
    to get a 3 × 3 identity matrix. C.2  Using MATLAB to Plot To plot using MATLAB is easy. For two-dimensional plot, use the plot command with two arguments as:
  • An Introduction to Applied Multivariate Analysis
    • Tenko Raykov, George A. Marcoulides(Authors)
    • 2008(Publication Date)
    • Routledge
      (Publisher)
    c). For example, if A and B are the first and second matrix in the middle part of the following Equation 2.7, respectively, their sum and difference are readily obtained as follows:
    and
    Addition and subtraction operations of conform matrices are straightforwardly extended to any number of matrices. We also note that matrices do not need to be square, as they happen to be in the last two examples, in order for their sum and difference to be defined (which is readily seen from their general definition given above), as long as they are of the same size.
    Matrix multiplication with a number (scalar). A matrix is multiplied with a number (scalar) by multiplying each element of the matrix with that number. That is, if the matrix A = [aij ] is considered, then gA = [gaij ], where g is a scalar (i = 1, 2, …, r; j = 1, 2, …, c). For example, if g = 3, and
    then
    We note that the order of matrix multiplication with a scalar does not matter. Accordingly, the following Equality holds for a given scalar g:
    (i = 1, 2, …, r; j = 1, 2, …, c). In addition, there is no requirement on matrix size, in order for either addition, subtraction, or multiplication with a scalar to be carried out; however, as indicated previously, for addition and subtraction to be possible the matrices involved must be of the same size.
    Matrix transposition. Oftentimes it is useful to consider the rows of a given matrix as columns of another one, and the columns of the former as rows of the latter. In other words, a new matrix of interest can be formed by interchanging the rows and columns of a given matrix. Such an exchange of the rows with the columns in a matrix is called matrix transposition, and is commonly denoted by a prime (); sometimes, instead of a prime, a superscripted letter T is used in the literature to denote the transpose of a matrix. That is, if A = [aij ], then its transpose matrix is denoted by A (or AT ) and is defined as A = [aji ] (i = 1, 2, …, r; j = 1, 2, …, c