How to multiply a 4x4 matrix with a 1x3 matrix in C?
If you already have a set 4x4 matrix. Ex.
Matrix A = [1 2 3 4;
5 6 7 8;`
9 10 11 12;
13 14 15 16]
Matrix B = [1, 2, 3]
How would you convert Matrix A into C coding? Also what would there positions be in code? For position I mean: if I'm trying to multiply the first row into matrix B, can I do this?
A[1][0]*B[0]+A[1][1]*B[1]+A[1][2]*B[2]
Outline code:
main(){
int matrixA[4][4] = [{"1","2","3","4"};
{"5","6","7","8"};
{"9","10","11","12"};
{"13","14","15","16"}];
printf(matrix A);
return 0;
}
First of all you cannot multiply a 1×3 matrix with 4×4 matrix. You should have matrices like m×n and n×p to get them multiplied (the result will be an m×p matrix).
Also for having a 4×4 matrix in C you should implement it like this:
int main()
{
int mat[4][4];
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
scanf("%d", &mat[i][j]);
}
}
return 0;
}
As far i can understand, you want to make a program to execute mathematical fractions related to matrices. Example in linear algebra. Matrix sizes are not checked, but you get the idea. In division you have to make the calculation of Array2^-1 to find it. Hope i helped. After you find it, multiply the result ,to Array1. In division however, things are more complicated. You need to approach your coding in different ways, depending to the array(matrix) dimensions you have. Exceptions must be included and need to read a bit of theory on how to divide those matrices. See https://en.wikipedia.org/wiki/Division_(mathematics) for more details.
#include <stdio.h>
#include <math.h>
int main(){
//counters
int i=0,j=0,k=0,sum=0;
//first array
int Array1[4][4] ={
{87,96,70,22},
{18,65,77,78},
{76,72,84,65},
{87,93,73,77}};
//second array
int Array2[4][4]={
{14,45,66,88},
{45,32,97,44},
{34,64,23,66},
{39,98,55,32}};
//result array.
int ResultArray[4][4];
// Add
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
ResultArray[i][j]= Array1[i][j]+Array2[i][j];
}
}
//result
printf("tAdd Array1 and Array2n");
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
printf(" %10d t",ResultArray[i][j]);
}
printf("n");
}
//subtract
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
ResultArray[i][j]= Array1[i][j]-Array2[i][j];
}
}
//result
printf("tSubtract Array1 and Array2n");
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
printf(" %10d t",ResultArray[i][j]);
}
printf("n");
}
//multiply
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
for (k = 0; k < 4; ++k) {
sum=sum+Array1[i][k]*Array2[k][j];
}
ResultArray[i][j]=sum;
sum=0;
}
}
//result
printf("tMultiplication Array1 and Array2n");
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
printf(" %10d t",ResultArray[i][j]);
}
printf("n");
}
return 0;
}
你可以这样写:
int matrixA[4][4] = {
{ 1, 2, 3, 4},//matrixA[0][0] is 1
{ 5, 6, 7, 8},//matrixA[1][1] is 6
{ 9, 10, 11, 12},
{13, 14, 15, 16}
};
int matrixB[] = { 1, 2, 3};
for(int r = 0; r < 4; ++r){
for(int c = 0; c < 4; ++c){
printf("%3d", matrixA[r][c]);
}
printf("n");
}
链接地址: http://www.djcxy.com/p/85992.html
上一篇: 矩阵乘法优化(OpenMP)