Basic algorithm of C language 33-basic operation of Matrix

Source: Internet
Author: User

Matrix Basics
/*
==================================================================
Title: Input matrix A, B, output a,b,a transpose matrix d,a*b,b+d=e;
==================================================================
*/
#include <stdio.h>
#define TJ1 for (i=0;i<2;i++)
#define TJ2 for (j=0;j<3;j++)
#define HH printf ("\ n")
void Main ()
{
int a[2][3],b[3][2],c[2][2],d[3][2],e[3][2];
int i,j,k;
Input matrix A
printf ("Input 2*3 matrix a:\n");
TJ1
TJ2
scanf ("%d", &a[i][j]);
Input Matrix B
printf ("\ n input 3*2 matrix b:\n");
TJ2
TJ1
scanf ("%d", &b[j][i]);
Output matrix A:
printf ("\n=====================\n output Matrix a:\n");
TJ1
{
TJ2
printf ("%4d", A[i][j]);
hh
}
printf ("\n=====================\na transpose Matrix d:\n");
TJ1
TJ2
D[J][I]=A[I][J];
TJ2
{
TJ1
printf ("%4d", D[j][i]);
printf ("\ n");
}
Output Matrix B:
printf ("\n\n=====================\n output Matrix b:\n");
TJ2
{
TJ1
printf ("%4d", B[j][i]);
hh
}
Matrix A * matrix B, saved to C
for (i=0;i<2;i++)
for (j=0;j<2;j++)
{
c[i][j]=0;
for (k=0;k<3;k++)
C[I][J]+=A[I][K]*B[K][J];
}
Output Matrix C:
printf ("\n=====================\n Matrix a[2][3]*b[3][2]=c[2][2], as follows: \ n");
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
printf ("%4d", C[i][j]);
printf ("\ n");
}
printf ("\n=====================\n matrix B + Matrix d:\n");
TJ2
TJ1
E[j][i]=b[j][i]+d[j][i];
TJ2
{
TJ1
printf ("%4d", E[j][i]);
hh
}
}
/*
====================================================================
Evaluation:
1, master the output of the method of line-changing;

2, transpose the matrix when the original matrix row and column Exchange, A[i][j]-->d[j][i], the original is 2 rows 3 columns, now with 3 rows and two columns output! That

I=0;i<3;i++;\j=0;j<2;j++;i and J are just symbols that do not affect the results, but only correspond to the d[rows [columns] in the output.

3, the addition is very simple, but must be the pass matrix;

4, multiplication is more complex, first define the initial matrix, assign initial value = 0; Then define the column that holds the product matrix, noting that the column is the largest

Value is the column of the left matrix, then left row * Right column, first column increment, then replace one row, repeat the above steps until the last row

Multiply the last column to the end!

=====================================================================
*/





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic algorithm of C language 33-basic operation of Matrix

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.