RE: calculating successive scalar product of two matrices

From: lavut (anonymous_at_discussions.microsoft.com)
Date: 03/09/04


Date: Mon, 8 Mar 2004 16:06:06 -0800


     
     ----- seia0106 wrote: -----
     
     Hello everyone,
     I am trying to write a function using For loops that should make one
     matrix slide over another bigger matrix and calculate scalar product
     in each step. For example here are the two matrices
     int A[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
     int B[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
     
     What i want is to put matrix B on top of Matrix A(starting top left),
     calculate scalar product of B and that portion(3x3) of A that lies
     under B, then move one column right , again calculate scalar product
     and repeat this process till last column and then do the same with
     row 2.
     What i am fiding difficult is how to write the For loops for this kind
     of operation.
     Can anyone please kindly provide some help in solving this problem. It
     may sound easy to many of you but is causing difficulty to me.
     Thanks in advance.
     

Hi, I think I've come up with this

#include <stdio.h>
int A[5][5]={
        { 1, 2, 3, 4, 5},
        { 6, 7, 8, 9,10},
        {11,12,13,14,15},
        {16,17,18,19,20},
        {21,22,23,24,25}
        };
int B[3][3] = {
        {1,2,3},
        {4,5,6},
        {7,8,9}
};

const int scA=sizeof(A[0])/sizeof(A[0][0]);
const int scB=sizeof(B[0])/sizeof(B[0][0]);

int getAI(int ii, int jj) {
        return scA*ii+jj;
}

void getMap(int start, int map[scB][scB]) {
        int s=start;
        for(int i=0; i<scB; i++) {
                int line=s/scA;
                int col=s%scA;
                for(int j=0;j<scB;j++) {
                        map[i][j]=getAI(line,col+j);
                }
                s=s+scA;
        }
}
void main(){
        int map[scB][scB];
        int scalarProduct[scB][scB];
        for(int iii=0; iii<=scA-scB; iii++) {
                for(int jjj=0; jjj<=scA-scB; jjj++) {
                        getMap(getAI(iii,jjj), map); //get mapping index from Bij to Aij starting at index AI
                        for(int i=0;i<scB;i++){
                                for(int j=0;j<scB;j++) {
                                        //printf("[%d][%d]=%d ", i,j,map[i][j]);
                                        int p=map[i][j]/scA; //line in A
                                        int q=map[i][j]%scA; //column in A
                                        scalarProduct[i][j]= A[p][q] *B[i][j] ;
                                        printf("[%d][%d]=%d ", i,j,scalarProduct[i][j]);
                                }
                                printf("\n");
                        }
                        printf("\n=======\n");
                }
        }
                                
}

and the output of the scalar product of A and B, with B a submatrix of A :
[0][0]=1 [0][1]=4 [0][2]=9
[1][0]=24 [1][1]=35 [1][2]=48
[2][0]=77 [2][1]=96 [2][2]=117

=======
[0][0]=2 [0][1]=6 [0][2]=12
[1][0]=28 [1][1]=40 [1][2]=54
[2][0]=84 [2][1]=104 [2][2]=126

=======
[0][0]=3 [0][1]=8 [0][2]=15
[1][0]=32 [1][1]=45 [1][2]=60
[2][0]=91 [2][1]=112 [2][2]=135

=======
[0][0]=6 [0][1]=14 [0][2]=24
[1][0]=44 [1][1]=60 [1][2]=78
[2][0]=112 [2][1]=136 [2][2]=162

=======
[0][0]=7 [0][1]=16 [0][2]=27
[1][0]=48 [1][1]=65 [1][2]=84
[2][0]=119 [2][1]=144 [2][2]=171

=======
[0][0]=8 [0][1]=18 [0][2]=30
[1][0]=52 [1][1]=70 [1][2]=90
[2][0]=126 [2][1]=152 [2][2]=180

=======
[0][0]=11 [0][1]=24 [0][2]=39
[1][0]=64 [1][1]=85 [1][2]=108
[2][0]=147 [2][1]=176 [2][2]=207

=======
[0][0]=12 [0][1]=26 [0][2]=42
[1][0]=68 [1][1]=90 [1][2]=114
[2][0]=154 [2][1]=184 [2][2]=216

=======
[0][0]=13 [0][1]=28 [0][2]=45
[1][0]=72 [1][1]=95 [1][2]=120
[2][0]=161 [2][1]=192 [2][2]=225

=======
Hope this is what you expected as result
Cheers
:-)



Relevant Pages