C#
 Computer >> コンピューター >  >> プログラミング >> C#

C#で2つの行列が同一かどうかを判定する方法

はじめに

2つの行列が完全に同一であるかどうかを判定するには、事前に「そもそも比較可能かどうか」を確認する必要があります。行列を比較できるためには、少なくとも両方の行列の次元(行数・列数)が一致していることが前提条件となります。次元が異なる行列同士は、要素を正しく対応付けて比較することができないからです。

ステップ1:比較可能性のチェック

まず、2つの行列の行数と列数がそれぞれ一致しているかどうかを確認します。もし一致していなければ、比較ができない旨のメッセージを出力します。

if (row1 != row2 || col1 != col2) {
    Console.Write("Matrices can't be compared:\n");
}

ステップ2:フラグ変数を使った同一性の判定

次元が一致している場合は、elseブロック内で各要素を順番に比較していきます。ここでは、初期値が 1 のフラグ変数 flag を用意し、対応する位置の要素が1つでも異なっていればフラグを 0 に設定してループを抜けます。最終的にフラグが 1 のまま残っていれば、2つの行列は同一であると判定できます。

if (row1 != row2 || col1 != col2) {
    Console.Write("Matrices can't be compared:\n");
} else {
    Console.Write("Comparison of Matrices: \n");
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col1; j++) {
            if (arr1[i, j] != arr2[i, j]) {
                flag = 0;
                break;
            }
        }
    }
    if (flag == 1)
        Console.Write("Our matrices are equal!\n\n");
    else
        Console.Write("Our matrices are not equal!");
}

C#による実装例

それでは、2つの行列が同一であるかどうかを判定する完全なコードを見てみましょう。このプログラムは、ユーザーから2つの行列のサイズと各要素を入力してもらい、入力された行列の内容を表示したうえで、比較結果を出力します。

using System;
namespace Demo {
    public class ApplicationOne {
        public static void Main() {
            int[] arr1 = new int[10, 10];
            int[] arr2 = new int[10, 10];
            int flag = 1;
            int i, j, row1, col1, row2, col2;
            Console.Write("Rows in the 1st matrix: ");
            row1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Columns in the 1st matrix: ");
            col1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Rows in the 2nd matrix: ");
            row2 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Columns in the 2nd matrix: ");
            col2 = Convert.ToInt32(Console.ReadLine());
            Console.Write("Elements in the first matrix:\n");
            for (i = 0; i < row1; i++) {
                for (j = 0; j < col1; j++) {
                    Console.Write("element - [{0}],[{1}] : ", i, j);
                    arr1[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            Console.Write("Elements in the second matrix:\n");
            for (i = 0; i < row2; i++) {
                for (j = 0; j < col2; j++) {
                    Console.Write("element - [{0}],[{1}] : ", i, j);
                    arr2[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            Console.Write("Matrix 1:\n");
            for (i = 0; i < row1; i++) {
                for (j = 0; j < col1; j++)
                    Console.Write("{0} ", arr1[i, j]);
                Console.Write("\n");
            }
            Console.Write("Matrix 2:\n");
            for (i = 0; i < row2; i++) {
                for (j = 0; j < col2; j++)
                    Console.Write("{0} ", arr2[i, j]);
                Console.Write("\n");
            }
            if (row1 != row2 || col1 != col2) {
                Console.Write("Matrices can't be compared:\n");
            } else {
                Console.Write("Comparison of Matrices: \n");
                for (i = 0; i < row1; i++) {
                    for (j = 0; j < col1; j++) {
                        if (arr1[i, j] != arr2[i, j]) {
                            flag = 0;
                            break;
                        }
                    }
                }
                if (flag == 1)
                    Console.Write("Our matrices are equal!\n\n");
                else
                    Console.Write("Our matrices are not equal!");
            }
        }
    }
}

実行結果

2つの行列の行数・列数およびすべての要素が一致している場合、プログラムは次のように「Our matrices are equal!(行列は等しい)」と出力します。

Rows in the 1st matrix: Columns in the 1st matrix: Rows in the 2nd matrix: Columns in the 2nd matrix: Elements in the first matrix:
Elements in the second matrix:
Matrix 1:
Matrix 2:
Comparison of Matrices: 
Our matrices are equal!

まとめ

このように、行列の同一性判定では「① 次元の一致確認 → ② 全要素の比較 → ③ フラグによる結果判定」という流れで処理を行います。要素が1つでも異なっていれば即座にループを抜けるため、無駄な比較を省きながら効率的に判定できるのがポイントです。

  1. Pythonで2つのリストが循環的に同一かどうかを判定する方法

    この記事では、2つのリストが与えられたとき、それらが循環的に同一(circularly identical)であるかどうかを判定するPythonプログラムを紹介します。循環的に同一とは、片方のリストの要素を回転させたとき、もう片方のリストと完全に一致する状態を指します。 実行例 入力 : A = [100, 100, 10, 10, 100] B = [100, 100, 100, 10, 10] 出力 : True 解説 リストAの要素を順番に循環的に回転させると、リストBと同じ並びになるため、結果は True となります。つまり、開始位置が異なるだけで、要素の並びのパター

  2. 【Python入門】2つの行列が同一かどうかを判定するプログラムの書き方

    この記事では、与えられた2つの行列(マトリックス)が同一であるかどうかを判定するPythonプログラムを紹介します。2つの行列が同一であるためには、次の条件を満たす必要があります。 両行列の行数・列数(次数)が一致していること 対応するすべての要素が等しいこと これらの条件を1つでも満たさない場合、2つの行列は同一とはみなされません。 アルゴリズム 判定の手順は以下の通りです。計算量は O(n²)(n×n行列の場合)となります。 ステップ1: 2つの行列を作成する。 ステップ2: 1つ目の行列と2つ目の行列のすべての要素を走査し、 対応する要素同士を順番に比較する