#include <stdio.h> #include <conio.h> int main() { int m , n , s , p , j = 0; float sum = 0 , x[20][20] , y[20][20] , z[20][20]; do { printf("Enter M & N & S & P to determine the matrix size X[M,N] , Y[S,P] (M=P & N=S) \n"); scanf(" %d %d %d %d", &m , &n , &s , &p ); } while ( (m < 0  n < 0)  ( s < 0  p < 0 )  ( m != p  n!= s )); { for ( int t = 0 ; t < m ; t++ ) { for( int u = 0 ; u < n ; u++ ) { printf(" x[%d][%d] ", t + 1 ,u + 1 ); scanf("%f", &x[t][u] ); } } for ( int t = 0 ; t < s ; t++ ) { for( int u = 0 ; u < p ; u++ ) { printf(" y[%d][%d] ", t + 1 , u + 1 ); scanf("%f", &x[s][p] ); } } } for ( int t = 0 ; t < n ; t++ ) { sum = 0; j = 0; while ( j < p ) { for( int u = 0 ; u < m ; u++ ) sum += x[u][t] * y[t][u + j]; z[t][j] = sum ; j++ ; } } for ( int t = 0 ; t < p ; t++ ) for ( int u = 0 ; u < m ; u++ ) { printf("%f\t", z[u][t] ); if ( u == p  1 ) printf("\n"); } getch(); }
multiplying matrices
Page 1 of 110 Replies  707 Views  Last Post: 23 December 2010  01:18 AM
#1
multiplying matrices
Posted 22 December 2010  01:40 PM
Replies To: multiplying matrices
#2
Re: multiplying matrices
Posted 22 December 2010  01:48 PM
1,2 9,3 9,6 3,4 * 1,2 = 1,8
#3
Re: multiplying matrices
Posted 22 December 2010  02:08 PM
Aphex19, on 22 December 2010  12:48 PM, said:
1,2 9,3 9,6 3,4 * 1,2 = 1,8
are you sure? I think rows should be multiplied by columns but I can't write the code correctly
This post has been edited by shahrooz: 22 December 2010  02:11 PM
#4
Re: multiplying matrices
Posted 22 December 2010  02:12 PM
1,2 9,3 11, 7 3,4 * 1,2 = 31,17
in general Matrix Multiplication is a little more complicated than you think.
#5
Re: multiplying matrices
Posted 22 December 2010  02:27 PM
#6
Re: multiplying matrices
Posted 22 December 2010  02:36 PM
#include <stdio.h> #include <conio.h> int main() { int m , n , s , p , j = 0; float sum = 0 , x[20][20] , y[20][20] , z[20][20]; do { printf("Enter M & N & S & P to determine the matrix size X[M,N] , Y[S,P] (M=P & N=S) \n"); scanf(" %d %d %d %d", &m , &n , &s , &p ); } while ( (m < 0  n < 0)  ( s < 0  p < 0 )  ( m != p  n!= s )); { for ( int t = 0 ; t < m ; t++ ) { for( int u = 0 ; u < n ; u++ ) { printf(" x[%d][%d] ", t + 1 ,u + 1 ); scanf("%f", &x[t][u] ); } } for ( int t = 0 ; t < s ; t++ ) { for( int u = 0 ; u < p ; u++ ) { printf(" y[%d][%d] ", t + 1 , u + 1 ); scanf("%f", &x[s][p] ); } } } for ( int t = 0 ; t < n ; t++ ) { sum = 0; j = 0; while ( j < p ) { for( int u = 0 ; u < m ; u++ ) { sum += x[u][t] * y[t][u + j]; z[t][j] = sum ; } j++ ; } } for ( int t = 0 ; t < p ; t++ ) for ( int u = 0 ; u < m ; u++ ) { printf("%f\t", z[u][t] ); if ( u == p  1 ) printf("\n"); } getch(); }
#7
Re: multiplying matrices
Posted 22 December 2010  03:08 PM
#8
Re: multiplying matrices
Posted 22 December 2010  03:33 PM
so I if I have the matrix:
A =  a b c   d e f 
element a is in A[0][0], where is element f?
A[2][1] or A[1][2]?
#9
Re: multiplying matrices
Posted 22 December 2010  05:32 PM
NickDMax, on 22 December 2010  02:33 PM, said:
so I if I have the matrix:
A =  a b c   d e f 
element a is in A[0][0], where is element f?
A[2][1] or A[1][2]?
I think f is A[3][2]
NickDMax, on 22 December 2010  02:08 PM, said:
omg, mistyped it, thanks. I'll give it another shot now
not working yet
#include <stdio.h> #include <conio.h> int main() { int m , n , s , p , j = 0; float sum = 0 , x[20][20] , y[20][20] , z[20][20]; do { printf("Enter M & N & S & P to determine the matrix size X[M,N] , Y[S,P] (M=P & N=S) \n"); scanf(" %d %d %d %d", &m , &n , &s , &p ); } while ( (m < 0  n < 0)  ( s < 0  p < 0 )  ( m != p  n!= s )); { for ( int t = 0 ; t < m ; t++ ) { for( int u = 0 ; u < n ; u++ ) { printf(" x[%d][%d] ", t + 1 ,u + 1 ); scanf("%f", &x[t][u] ); } } for ( int t = 0 ; t < s ; t++ ) { for( int u = 0 ; u < p ; u++ ) { printf(" y[%d][%d] ", t + 1 , u + 1 ); scanf("%f", &y[s][p] ); } } } for ( int t = 0 ; t < n ; t++ ) { sum = 0; j = 0; while ( j < p ) { for( int u = 0 ; u < m ; u++ ) { sum += x[u][t] * y[t][u + j]; z[t][j] = sum ; } j++ ; } } for ( int t = 0 ; t < p ; t++ ) for ( int u = 0 ; u < m ; u++ ) { printf("%f\t", z[u][t] ); if ( u == p  1 ) printf("\n"); } getch(); }
This post has been edited by shahrooz: 22 December 2010  05:29 PM
#10
Re: multiplying matrices
Posted 22 December 2010  09:30 PM
#11
Re: multiplying matrices
Posted 23 December 2010  01:18 AM
For starters, I suggest that you use specific matrices with small size[2x2 or 3x3] in order to get the multiplication correct. Now. When we do a matrix multiplication, there is a pattern. From the given link by NickDMax,
If we make further observation for each element, it seems the first matrix have the same row and second matrix have the same column. In the normal nested for loop, maybe we more variable to deal with it[hint: maybe 2 instead of one, each dealing with row and column]. Calculate the sum in form of for loop.
This post has been edited by shad0wk1: 23 December 2010  01:21 AM
