Convert given lower triangular Matrix to 1D array
Last Updated :
23 Dec, 2022
Given a lower triangular matrix M[][] of dimension N * N, the task is to convert it into a one-dimensional array by storing only non-zero elements.
Examples:
Input: M[][] = {{1, 0, 0, 0}, {2, 3, 0, 0}, {4, 5, 6, 0}, {7, 8, 9, 10}}
Output:
Row-wise: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Column-wise: {1, 2, 4, 7, 3, 5, 8, 6, 9, 10}
Explanation: All the non-zero elements of the matrix are {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
Arranging these elements in row-wise manner in a 1D array generates the sequence {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
Arranging these elements in column-wise manner in a 1D array generates the sequence {1, 2, 4, 7, 3, 5, 8, 6, 9, 10}.
Input: M[][] = {{1, 0, 0, }, {2, 3, 0}, {4, 5, 6}}
Output:
Row-wise: {1, 2, 3, 4, 5, 6}
Column-wise: {1, 2, 4, 3, 5, 6}
Approach:To convert a 2-dimensional matrix to a 1-dimensional array following two methods are used:
Row – Major Order:
- In this method, adjacent elements of a row are placed next to each other in the array.
- The following formula is used to find out the respective positions of the non-zero elements of the lower triangular matrix in the 1-dimensional array.
Index of matrix element at position (i, j) = ((i * (i – 1))/2 + j – 1)
where 1 ? i, j ? N and i ? j
Column – Major Order:
- In this method, consecutive elements of a column are placed adjacently in the array.
- The following formula is used to find out the respective positions of the non-zero elements of the lower triangular matrix in the 1-dimensional array.
Index of matrix element at position (i, j) = (N * (j – 1) – ((j – 2) * (j – 1))/2) + (i – j)
where 1 ? i, j ? N and i ? j.
Follow the steps below to solve the problem:
- Initialize an array, say A[], to store the non-zero elements of the matrix.
- Traverse the matrix M[][] and find the index of non-zero elements of the matrix in the array A[] using the formula for row-major mapping and insert each non-zero element in the array A[].
- After completing the above step, print the array A[] for row-major mapping.
- Again, traverse the matrix M[][] and find the index of non-zero elements of the matrix in the array A[] using the formula for column-major mapping and insert each non-zero element in the array A[].
- After completing the above steps, print the array A[] for column-major mapping.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class LTMatrix {
private :
int n;
int * A;
int tot;
public :
LTMatrix( int N)
{
this ->n = N;
tot = N * (N + 1) / 2;
A = new int [N * (N + 1) / 2];
}
~LTMatrix() { delete [] A; }
void Display( bool row = true );
void setRowMajor( int i, int j, int x);
void setColMajor( int i, int j, int x);
int getN() { return n; }
};
void LTMatrix::setColMajor( int i, int j, int x)
{
if (i >= j) {
int index
= (n * (j - 1) - (((j - 2) * (j - 1)) / 2))
+ (i - j);
A[index] = x;
}
}
void LTMatrix::setRowMajor( int i, int j, int x)
{
if (i >= j) {
int index = (i * (i - 1)) / 2 + j - 1;
A[index] = x;
}
}
void LTMatrix::Display( bool row)
{
for ( int i = 0; i < tot; i++) {
cout << A[i] << " " ;
}
cout << endl;
}
void displayRowMajor( int N)
{
LTMatrix rm(N);
rm.setRowMajor(1, 1, 1);
rm.setRowMajor(2, 1, 2);
rm.setRowMajor(2, 2, 3);
rm.setRowMajor(3, 1, 4);
rm.setRowMajor(3, 2, 5);
rm.setRowMajor(3, 3, 6);
rm.setRowMajor(4, 1, 7);
rm.setRowMajor(4, 2, 8);
rm.setRowMajor(4, 3, 9);
rm.setRowMajor(4, 4, 10);
cout << "Row-Wise:\n" ;
rm.Display();
}
void displayColMajor( int N)
{
LTMatrix cm(N);
cm.setColMajor(1, 1, 1);
cm.setColMajor(2, 1, 2);
cm.setColMajor(2, 2, 3);
cm.setColMajor(3, 1, 4);
cm.setColMajor(3, 2, 5);
cm.setColMajor(3, 3, 6);
cm.setColMajor(4, 1, 7);
cm.setColMajor(4, 2, 8);
cm.setColMajor(4, 3, 9);
cm.setColMajor(4, 4, 10);
cout << "Column-Wise:\n" ;
cm.Display( false );
}
int main()
{
int N = 4;
displayRowMajor(N);
displayColMajor(N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static class LTMatrix {
static int n;
static int A[];
static int tot;
LTMatrix( int N)
{
this .n = N;
tot = N * (N + 1 ) / 2 ;
A = new int [N * (N + 1 ) / 2 ];
}
static void Display( boolean row)
{
for ( int i = 0 ; i < tot; i++) {
System.out.print(A[i] + " " );
}
System.out.println();
}
static void setRowMajor( int i, int j, int x)
{
if (i >= j) {
int index = (i * (i - 1 )) / 2 + j - 1 ;
A[index] = x;
}
}
static void setColMajor( int i, int j, int x)
{
if (i >= j) {
int index = (n * (j - 1 )
- (((j - 2 ) * (j - 1 )) / 2 ))
+ (i - j);
A[index] = x;
}
}
static int getN() { return n; }
}
static void displayRowMajor( int N)
{
LTMatrix rm = new LTMatrix(N);
rm.setRowMajor( 1 , 1 , 1 );
rm.setRowMajor( 2 , 1 , 2 );
rm.setRowMajor( 2 , 2 , 3 );
rm.setRowMajor( 3 , 1 , 4 );
rm.setRowMajor( 3 , 2 , 5 );
rm.setRowMajor( 3 , 3 , 6 );
rm.setRowMajor( 4 , 1 , 7 );
rm.setRowMajor( 4 , 2 , 8 );
rm.setRowMajor( 4 , 3 , 9 );
rm.setRowMajor( 4 , 4 , 10 );
System.out.println( "Row-Wise:" );
rm.Display( false );
}
static void displayColMajor( int N)
{
LTMatrix cm = new LTMatrix(N);
cm.setColMajor( 1 , 1 , 1 );
cm.setColMajor( 2 , 1 , 2 );
cm.setColMajor( 2 , 2 , 3 );
cm.setColMajor( 3 , 1 , 4 );
cm.setColMajor( 3 , 2 , 5 );
cm.setColMajor( 3 , 3 , 6 );
cm.setColMajor( 4 , 1 , 7 );
cm.setColMajor( 4 , 2 , 8 );
cm.setColMajor( 4 , 3 , 9 );
cm.setColMajor( 4 , 4 , 10 );
System.out.println( "Column-Wise:" );
cm.Display( false );
}
public static void main(String[] args)
{
int N = 4 ;
displayRowMajor(N);
displayColMajor(N);
}
}
|
Python3
class LTMatrix:
def __init__( self , N):
self .n = N;
self .tot = N * (N + 1 ) / / 2 ;
self .A = [ None ] * ( int (N * (N + 1 ) / 2 ));
def Display( self , row):
for i in range ( int ( self .tot)):
print ( self .A[i], end = " " )
print ()
def setRowMajor( self , i, j, x):
if (i > = j):
index = (i * (i - 1 )) / / 2 + j - 1 ;
self .A[index] = x;
def setColMajor( self , i, j, x):
if (i > = j) :
index = int (( self .n * (j - 1 )
- (((j - 2 ) * (j - 1 )) / 2 ))
+ (i - j));
self .A[index] = x;
def getN( self ):
return self .n;
def displayRowMajor(N):
rm = LTMatrix(N);
rm.setRowMajor( 1 , 1 , 1 );
rm.setRowMajor( 2 , 1 , 2 );
rm.setRowMajor( 2 , 2 , 3 );
rm.setRowMajor( 3 , 1 , 4 );
rm.setRowMajor( 3 , 2 , 5 );
rm.setRowMajor( 3 , 3 , 6 );
rm.setRowMajor( 4 , 1 , 7 );
rm.setRowMajor( 4 , 2 , 8 );
rm.setRowMajor( 4 , 3 , 9 );
rm.setRowMajor( 4 , 4 , 10 );
print ( "Row-Wise:" );
rm.Display( False );
def displayColMajor(N):
cm = LTMatrix(N);
cm.setColMajor( 1 , 1 , 1 );
cm.setColMajor( 2 , 1 , 2 );
cm.setColMajor( 2 , 2 , 3 );
cm.setColMajor( 3 , 1 , 4 );
cm.setColMajor( 3 , 2 , 5 );
cm.setColMajor( 3 , 3 , 6 );
cm.setColMajor( 4 , 1 , 7 );
cm.setColMajor( 4 , 2 , 8 );
cm.setColMajor( 4 , 3 , 9 );
cm.setColMajor( 4 , 4 , 10 );
print ( "Column-Wise:" );
cm.Display( False );
N = 4 ;
displayRowMajor(N);
displayColMajor(N);
|
C#
using System;
public class LTMatrix {
static int n;
static int [] A;
static int tot;
public LTMatrix( int N)
{
n = N;
tot = N * (N + 1) / 2;
A = new int [N * (N + 1) / 2];
}
public void Display(Boolean row)
{
for ( int i = 0; i < tot; i++) {
Console.Write(A[i] + " " );
}
Console.Write( "" );
}
public void setRowMajor( int i, int j, int x)
{
if (i >= j) {
int index = (i * (i - 1)) / 2 + j - 1;
A[index] = x;
}
}
public void setColMajor( int i, int j, int x)
{
if (i >= j) {
int index
= (n * (j - 1) - (((j - 2) * (j - 1)) / 2))
+ (i - j);
A[index] = x;
}
}
static int getN() { return n; }
}
class GFG {
static void displayRowMajor( int N)
{
LTMatrix rm = new LTMatrix(N);
rm.setRowMajor(1, 1, 1);
rm.setRowMajor(2, 1, 2);
rm.setRowMajor(2, 2, 3);
rm.setRowMajor(3, 1, 4);
rm.setRowMajor(3, 2, 5);
rm.setRowMajor(3, 3, 6);
rm.setRowMajor(4, 1, 7);
rm.setRowMajor(4, 2, 8);
rm.setRowMajor(4, 3, 9);
rm.setRowMajor(4, 4, 10);
Console.WriteLine( "Row-Wise:" );
rm.Display( false );
}
static void displayColMajor( int N)
{
LTMatrix cm = new LTMatrix(N);
cm.setColMajor(1, 1, 1);
cm.setColMajor(2, 1, 2);
cm.setColMajor(2, 2, 3);
cm.setColMajor(3, 1, 4);
cm.setColMajor(3, 2, 5);
cm.setColMajor(3, 3, 6);
cm.setColMajor(4, 1, 7);
cm.setColMajor(4, 2, 8);
cm.setColMajor(4, 3, 9);
cm.setColMajor(4, 4, 10);
Console.WriteLine( "\nColumn-Wise:" );
cm.Display( false );
}
public static void Main()
{
int N = 4;
displayRowMajor(N);
displayColMajor(N);
}
}
|
Javascript
class LTMatrix {
constructor(N)
{
this .n = N;
this .tot = N * (N + 1) / 2;
this .A = new Array(Math.floor(N * (N + 1) / 2));
}
Display(row)
{
for ( var i = 0; i < this .tot; i++) {
process.stdout.write( this .A[i] + " " );
}
console.log();
}
setRowMajor(i, j, x)
{
if (i >= j) {
let index = (i * (i - 1)) / 2 + j - 1;
this .A[index] = x;
}
}
setColMajor(i, j, x)
{
if (i >= j) {
var index
= Math.floor(( this .n * (j - 1)
- (((j - 2) * (j - 1)) / 2))
+ (i - j));
this .A[index] = x;
}
}
getN() { return this .n; }
}
function displayRowMajor(N)
{
let rm = new LTMatrix(N);
rm.setRowMajor(1, 1, 1);
rm.setRowMajor(2, 1, 2);
rm.setRowMajor(2, 2, 3);
rm.setRowMajor(3, 1, 4);
rm.setRowMajor(3, 2, 5);
rm.setRowMajor(3, 3, 6);
rm.setRowMajor(4, 1, 7);
rm.setRowMajor(4, 2, 8);
rm.setRowMajor(4, 3, 9);
rm.setRowMajor(4, 4, 10);
console.log( "Row-Wise:" );
rm.Display( false );
}
function displayColMajor(N)
{
let cm = new LTMatrix(N);
cm.setColMajor(1, 1, 1);
cm.setColMajor(2, 1, 2);
cm.setColMajor(2, 2, 3);
cm.setColMajor(3, 1, 4);
cm.setColMajor(3, 2, 5);
cm.setColMajor(3, 3, 6);
cm.setColMajor(4, 1, 7);
cm.setColMajor(4, 2, 8);
cm.setColMajor(4, 3, 9);
cm.setColMajor(4, 4, 10);
console.log( "Column-Wise:" );
cm.Display( false );
}
let N = 4;
displayRowMajor(N);
displayColMajor(N);
|
Output:
Row-Wise:
1 2 3 4 5 6 7 8 9 10
Column-Wise:
1 2 4 7 3 5 8 6 9 10
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...