📌  相关文章
📜  将给定的下三角矩阵转换为一维数组

📅  最后修改于: 2021-05-17 16:49:16             🧑  作者: Mango

给定维度为N * N的下三角矩阵M [] [] ,任务是通过仅存储非零元素将其转换为一维数组。

例子:

方法:使用以下两种方法将二维矩阵转换为一维数组:

行–主要订单

  • 在这种方法中,一行的相邻元素在数组中彼此相邻放置。

  • 以下公式用于找出一维数组中下三角矩阵的非零元素的各个位置。

专栏–主要订单

  • 在这种方法中,一列的连续元素在数组中相邻放置。

  • 以下公式用于找出一维数组中下三角矩阵的非零元素的各个位置。

请按照以下步骤解决问题:

  • 初始化一个数组,例如A [] ,以存储矩阵的非零元素。
  • 使用行主映射公式,遍历矩阵M [] []并找到数组A []中矩阵的非零元素的索引 并将每个非零元素插入数组A []中
  • 完成上述步骤后,打印用于行主要映射的数组A []
  • 同样,遍历矩阵M [] []并使用列主映射的公式在数组A []中找到矩阵非零元素的索引,并将每个非零元素插入数组A []中
  • 完成上述步骤后,打印用于列主映射的数组A []

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Class of Lower Triangular Matrix
class LTMatrix {
 
private:
    // Size of Matrix
    int n;
 
    // Pointer
    int* A;
 
    // Stores the count of non-zero
    // elements
    int tot;
 
public:
    // Constructor
    LTMatrix(int N)
    {
        this->n = N;
        tot = N * (N + 1) / 2;
        A = new int[N * (N + 1) / 2];
    }
 
    // Destructor
    ~LTMatrix() { delete[] A; }
 
    // Function to display array
    void Display(bool row = true);
 
    // Function to generate array
    // in Row - Major order
    void setRowMajor(int i, int j, int x);
 
    // Function to generate array
    // in Column - Major order
    void setColMajor(int i, int j, int x);
 
    // Function to find size of array
    int getN() { return n; }
};
 
// Function to generate array from
// given matrix by storing elements
// in column major order
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;
    }
}
 
// Function to generate array from
// given matrix by storing elements
// in row major order
void LTMatrix::setRowMajor(
    int i, int j, int x)
{
    if (i >= j) {
        int index = (i * (i - 1)) / 2
                    + j - 1;
        A[index] = x;
    }
}
 
// Function to display array elements
void LTMatrix::Display(bool row)
{
    for (int i = 0; i < tot; i++) {
        cout << A[i] << " ";
    }
    cout << endl;
}
 
// Function to generate and display
// array in Row-Major Order
void displayRowMajor(int N)
{
    LTMatrix rm(N);
 
    // Generate the array in the
    // row-major form
    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);
 
    // Display array elements
    // in row-major order
    cout << "Row-Wise:\n";
 
    rm.Display();
}
 
// Function to generate and display
// array in Column-Major Order
void displayColMajor(int N)
{
    LTMatrix cm(N);
 
    // Generate array in
    // column-major form
    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);
 
    // Display array elements
    // in column-major form
    cout << "Column-Wise:\n";
    cm.Display(false);
}
 
// Driver Code
int main()
{
    // Size of row or column
    // of square matrix
    int N = 4;
 
    // Function Call for row major
    // mapping
    displayRowMajor(N);
 
    // Function Call for column
    // major mapping
    displayColMajor(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
    // Class of Lower Triangular Matrix
    static class LTMatrix
    {
 
        // Size of Matrix
        static int n;
 
        // Pointer
        static int A[];
 
        // Stores the count of non-zero
        // elements
        static int tot;
 
        // Constructor
        LTMatrix(int N)
        {
            this.n = N;
            tot = N * (N + 1) / 2;
            A = new int[N * (N + 1) / 2];
        }
 
        // Function to display array elements
        static void Display(boolean row)
        {
            for (int i = 0; i < tot; i++)
            {
                System.out.print(A[i] + " ");
            }
            System.out.println();
        }
 
        // Function to generate array from
        // given matrix by storing elements
        // in row major order
        static void setRowMajor(int i, int j, int x)
        {
            if (i >= j) {
                int index = (i * (i - 1)) / 2 + j - 1;
                A[index] = x;
            }
        }
 
        // Function to generate array from
        // given matrix by storing elements
        // in column major order
        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;
            }
        }
 
        // Function to find size of array
        static int getN() { return n; }
    }
 
    // Function to generate and display
    // array in Row-Major Order
    static void displayRowMajor(int N)
    {
        LTMatrix rm = new LTMatrix(N);
 
        // Generate the array in the
        // row-major form
        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);
 
        // Display array elements
        // in row-major order
        System.out.println("Row-Wise:");
        rm.Display(false);
    }
 
    // Function to generate and display
    // array in Column-Major Order
    static void displayColMajor(int N)
    {
        LTMatrix cm = new LTMatrix(N);
 
        // Generate array in
        // column-major form
        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);
 
        // Display array elements
        // in column-major form
        System.out.println("Column-Wise:");
        cm.Display(false);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
       
        // Size of row or column
        // of square matrix
        int N = 4;
 
        // Function Call for row major
        // mapping
        displayRowMajor(N);
 
        // Function Call for column
        // major mapping
        displayColMajor(N);
    }
}
 
// This code is contributed by Dharanendra L V.


输出:
Row-Wise:
1 2 3 4 5 6 7 8 9 10 
Column-Wise:
1 2 4 7 3 5 8 6 9 10

时间复杂度: O(N 2 )
辅助空间: O(N 2 )