Showing posts with label Matrices. Show all posts
Showing posts with label Matrices. Show all posts

Some Operations on Matrix

A few days back someone asked me a question via email which I thought might
be useful to others too. So I’m listing that question along with its answer
below.


Q. I want to write a program such that users enter the value of matrix
and each operation (listed below) is performed by functions. I want to use switch
structure to call the functions.


1. Rotate the matrix around the diagonal.


Example:


   1 2 3 ---> 1 4 7
4 5 6 2 5 8
7 8 9 3 6 9

2. Rotate the matrix around the middle row.


Example:


   1 2 3 ---> 7 8 9
4 5 6 4 5 6
7 8 9 1 2 3

3. Rotate the matrix around the middle column.


Example:


   1 2 3 ---> 3 2 1
4 5 6 6 5 4
7 8 9 9 8 7

4. Set the upper triangle to zero.


Example:


   1 2 3 ---> 1 0 0
4 5 6 4 5 0
7 8 9 7 8 9

Ans. The following program does it. Please note that the matrix
is declared as global so as to reduce complications in the program. Better way
should have been to pass the matrix (local) to the functions from main().



// Program that does some Rotations
// about certain axes in two
// dimensional arrays
#include <iostream.h>

// change this to hold more
// values in the array
#define MAX 3

// it is declared to be global
// so that every function can access it
int ar[MAX][MAX];

// function prototypes
void EnterVal();
void rDiagonal();
void rMidRow();
void rMidCol();
void sUpperZero();
void Show();

void main()
{
int ch;

// loop until 'quit' is not selected
while(ch!=6)
{
cout<<"1> Enter Values\n";
cout<<"2> Rotate around Diagonal\n";
cout<<"3> Rotate around the Middle row\n";
cout<<"4> Rotate around the Middle column\n";
cout<<"5> Set the Upper triangle to zero\n";
cout<<"6> Quit\n\n";

cin>>ch;

// do as per choice
switch(ch)
{
case 1:
EnterVal();
break;

case 2:
rDiagonal();
Show();
break;

case 3:
rMidRow();
Show();
break;

case 4:
rMidCol();
Show();
break;

case 5:
sUpperZero();
Show();
break;
}
}
}

// --------------------
// Function Definitions
void EnterVal()
{
int i,j;

cout<<"Enter Values\n";

for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
cin>>ar[i][j];
}

void rDiagonal()
{
int temp,i,j;

for(i=0;i<MAX;i++)
for(j=i;j<MAX;j++)
{
temp=ar[i][j];
ar[i][j]=ar[j][i];
ar[j][i]=temp;
}
}

void rMidRow()
{
int temp,i,j;

for(i=0;i<(MAX-1)/2;i++)
for(j=0;j<MAX;j++)
{
temp=ar[(MAX-1)-i][j];
ar[(MAX-1)-i][j]=ar[i][j];
ar[i][j]=temp;
}
}

void rMidCol()
{
int temp,i,j;

for(i=0;i<MAX;i++)
for(j=0;j<(MAX-1)/2;j++)
{
temp=ar[i][(MAX-1)-j];
ar[i][(MAX-1)-j]=ar[i][j];
ar[i][j]=temp;
}
}

void sUpperZero()
{
int i,j;

for(i=0;i<(MAX+1)/2;i++)
for(j=i;j<MAX-i;j++)
ar[i][j]=0;
}

void Show()
{
int i,j;

for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
cout<<ar[i][j]<<" ";

cout<<endl;
}
cout<<"\n\n";
}


Related Articles:


Merging Two-Dimensional Arrays (Matrices)

In the article Merging
One Dimensional Arrays
, we discussed how to merge one-dimensional
arrays, in this article we will be discussing about the merging of two-dimensional
arrays. Merging as you know is the process of combining two similar things.
In the context of arrays, it means to form a big array from two smaller arrays
which has all the elements from both the arrays.


In case of one-dimensional arrays there is only one way in which two arrays
can be merged but in case of two-dimensional arrays there are two ways.


Suppose, we have the following two 2d arrays (matrices):


mat1={
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}

and

mat2={
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
}

then they can be merged in the following two ways:

merge_row={
{1, 2, 3, 10, 11, 12},
{4, 5, 6, 13, 14, 15},
{7, 8, 9, 16, 17, 18}
}

merge_col={
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
}

Now that you know the funda, we move on to the example program to illustrate
this:


  // example program to illustrate
// merging of two-dimensional or
// 2d arrays(matrices)
#include<iostream.h>

#define ROW1 3
#define ROW2 3
#define COL1 3
#define COL2 3

void main(void)
{
int i,j;
int matrix1[ROW1][COL1];
int matrix2[ROW2][COL2];

// these will store merged
// matrices
int merge_row[ROW1][COL1+COL2];
int merge_col[ROW1+ROW2][COL1];

// input the elements
cout<<"Input elements for MATRIX 1:\n";
for(i=0;i<ROW1;i++)
{
cout<<"ROW "<<i+1<<":\n";
for(j=0;j<COL1;j++)
cin>>matrix1[i][j];
}

cout<<"\nInput elements for MATRIX 2:\n";
for(i=0;i<ROW2;i++)
{
cout<<"ROW "<<i+1<<":";
for(j=0;j<COL2;j++)
cin>>matrix2[i][j];
}
//input complete

// merge matrices
// row merge starts
for(i=0;i<ROW1;i++)
for(j=0;j<COL1;j++)
merge_row[i][j]=matrix1[i][j];

for(i=0;i<ROW2;i++)
for(j=0;j<COL2;j++)
merge_row[i][j+COL2]=matrix2[i][j];
// row merge complete

// column merge starts
for(i=0;i<ROW1;i++)
for(j=0;j<COL1;j++)
merge_col[i][j]=matrix1[i][j];

for(i=0;i<ROW2;i++)
for(j=0;j<COL2;j++)
merge_col[i+ROW1][j]=matrix2[i][j];
// column merge complete

// show the merged martices
cout<<"\nROW Merge:\n\n";
for(i=0;i<ROW1;i++)
{
for(j=0;j<COL1+COL2;j++)
cout<<" "<<merge_row[i][j];

cout<<endl;
}

cout<<"\nCOLUMN Merge:\n\n";
for(i=0;i<ROW1+ROW2;i++)
{
for(j=0;j<COL1;j++)
cout<<" "<<merge_col[i][j];

cout<<endl;
}
}

Good-Bye!


Related Articles:


Algebra of Matrices (2D Arrays) Part II

As you know from the previous article, matrices are 2D arrays. In the previous
article, we saw how two matrices are added and subtracted. In this article,
we will continue our discussion on algebra of two matrices by discussing how
two matrices are multiplied.


Multiplication of two Matrices


Multiplication of two matrices mat1[a x b] and mat2[p x q] is only valid if
b=p. While there are many algorithms by which two matrices can be multiplied,
here I’ll give you the most simple algorithm. Others are used when efficiency
matters.


Algorithm for Multiplication of two Matrices


Suppose,




  • Two 2D arrays to be mat1 [p][p] and mat2 [p][p] having same number of rows
    and columns.




  • A third 2D array, mul [p][p] to store the result.




Here is the algorithm:


  1. FOR I = 0 TO (p-1)


  2. FOR J = 0 TO (p-1)
    mul [I][J] = 0
    FOR K = 0 TO (p-1)
    mul [I][J] += (mat1 [I][K] * mat2 [K][J])


  3. END OF INNER LOOP


  4. END OF OUTER LOOP



Below is a example program which illustrates the multiplication of two matrices
by this algorithm:


  // Example program in C++
// It shows you how to
// multiply two matrices
#include<iostream.h>

void main(void)
{
int i,j,k;
// this constant determines
// the number of rows and columns
const int max=3;

int mat1[max][max];
int mat2[max][max];
int mul[max][max];

cout<<"enter elements for mat1:";
for(i=0;i<=(max-1);i++)
for(j=0;j<=(max-1);j++)
cin>>mat1[i][j];

cout<<"enter elements for mat2:";
for(i=0;i<=(max-1);i++)
for(j=0;j<=(max-1);j++)
cin>>mat2[i][j];

// multiplication of two matrices
// is done here
for(i=0;i<=(max-1);i++)
for(j=0;j<=(max-1);j++)
{
mul[i][j]=0;
for(k=0;k<=(max-1);k++)
mul[i][j]+=mat1[i][k]*mat2[k][j];
}
// till here

// show the contents of the array
// after formatting
for(i=0;i<=(max-1);i++)
{
for(j=0;j<=(max-1);j++)
cout<<mul[i][j]<<" ";
cout<<endl;
}
}

Good-Bye!


Related Articles:


Algebra of Matrices (2D Arrays)

In the programming sense, Matrices are Two Dimensional or 2D arrays. Just as
Matrices have rows and columns, similarly 2D arrays too have rows and columns.


There are many mathematical operations like addition, subtraction, multiplication
etc. which can be performed on matrices, and therefore to 2D arrays also. In
this article, we will be discussing about the addition and subtraction of two
2D arrays (Matrices).


Addition of two Matrices (2D arrays)


For addition of two matrices both the matrices must have the same dimension.
Ex. if matrice one has the dimension p x q then matrice two must have the dimension
p x q.


In the addition process, each of the element of the first matrice is added
to the corresponding element of the second matrice and result is stored in the
third matrice having the same dimension (i.e. p x q). Below is the algorithm
for adding two matrices.


Algorithm for adding two Matrices


Suppose,




  • Two 2D arrays to be mat1 [p][q] and mat2 [p][q] having p rows and q columns
    respectively.




  • A third 2D array, sum [p][q] to store the result.




Here is the algorithm:




  1. FOR I = 0 TO (p-1)



  2. FOR J = 0 TO (q-1)



  3. sum [i][j] = mat1 [i][j] + mat2 [i][j]



  4. END OF INNER LOOP



  5. END OF OUTER LOOP



Subtraction of two Matrices


For subtraction, same rule applies. The process is also the same as for addition;
we just need to use the subtraction operator instead of the addition operator
;-)


Algorithm for subtracting two Matrices




  1. FOR I = 0 TO (p-1)



  2. FOR J = 0 TO (q-1)



  3. sum [i][j] = mat1 [i][j] - mat2 [i][j]



  4. END OF INNER LOOP



  5. END OF OUTER LOOP



Here is the program that illustrates the implementation of both of these algorithms:


  // Example program in C++
// It shows you how to add
// and subtract two matrices
#include<iostream.h>

void main(void)
{
int i,j,ch;
int mat1[3][3];
int mat2[3][3];
int sum[3][3];

cout<<"enter elements for mat1:";
for(i=0;i<=(3-1);i++)
for(j=0;j<=(3-1);j++)
cin>>mat1[i][j];

cout<<"enter elements for mat2:";
for(i=0;i<=(3-1);i++)
for(j=0;j<=(3-1);j++)
cin>>mat2[i][j];

cout<<"what do you want to do\n\n";
cout<<"1>addition\n2>subtraction\n";
cin>>ch;

switch(ch)
{
case 1:
// addition of matrices
// is done here
for(i=0;i<=(3-1);i++)
for(j=0;j<=(3-1);j++)
sum[i][j]=mat1[i][j]+mat2[i][j];
// till here
break;

case 2:
// subtraction of matrices
// is done here
for(i=0;i<=(3-1);i++)
for(j=0;j<=(3-1);j++)
sum[i][j]=mat1[i][j]-mat2[i][j];
// till her
break;
}

// sum is shown
for(i=0;i<=(3-1);i++)
for(j=0;j<=(3-1);j++)
cout<<"\n"<<sum[i][j];

cout<<endl;
}

Good-Bye for now!



Related Articles:


Check out this stream