zoukankan      html  css  js  c++  java
  • 矩阵转置

    矩阵转置 - C语言 C++ Java Python

    输入矩阵的行和列以及矩阵各个元素,输出它的转置矩阵。
    C语言:

    #include <stdio.h>
    
    int main()
    {
    	int line, colums;
    	printf("输入矩阵行列
    ");
    	scanf("%d%d", &line, &colums);
    	int array[line][colums];
    	for(int i = 0;i < line;i++)
    		for(int j = 0;j < colums;j++)
    			scanf("%d", &array[i][j]);
    	printf("
    原矩阵
    ");		
    	for(int i = 0;i < line;i++){
    		for(int j = 0;j < colums;j++)
    			printf("%d ", array[i][j]);
    		printf("
    ");
    	}
    	printf("
    转置矩阵
    ");		
    	for(int j = 0;j < colums;j++){
    		for(int i = 0;i < line;i++)
    			printf("%d ", array[i][j]);
    		printf("
    ");
    	}	
    	
    	return 0;
    } 
    /* Code Running Results
    输入矩阵行列
    2 3
    1 2 3
    4 5 6
    
    原矩阵
    1 2 3
    4 5 6
    
    转置矩阵
    1 4
    2 5
    3 6
    */ 
    

    以下 C++ 的代码与 C语言 几乎是一样的,是一样的道理,都是使用二维数组来实现矩阵的转置,只是输入输出换了一种编写方式。
    C++:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int line, colums;
    	cout<<"输入矩阵行列"<<endl;
    	cin>>line>>colums;
    	int array[line][colums];
    	for(int i = 0;i < line;i++)
    		for(int j = 0;j < colums;j++)
    			cin>>array[i][j];
    	cout<<endl<<"原矩阵"<<endl;		
    	for(int i = 0;i < line;i++){
    		for(int j = 0;j < colums;j++)
    			cout<<array[i][j]<<" "; 
    		cout<<endl;
    	}
    	cout<<endl<<"转置矩阵"<<endl;
    	for(int j = 0;j < colums;j++){
    		for(int i = 0;i < line;i++)
    			cout<<array[i][j]<<" "; 
    		cout<<endl;
    	}	
    	
    	return 0;
    } 
    /* Code Running Results
    输入矩阵行列
    2 3
    1 2 3
    4 5 6
    
    原矩阵
    1 2 3
    4 5 6
    
    转置矩阵
    1 4
    2 5
    3 6
    */ 
    

    Java 自然也可以使用二维数组实现矩阵的转置,但是那样大可不必再重复一遍。
    可以换一种方式,把二维数组换成存放数组列表的数组列表 ( 即 ArrayList<ArrayList> ) 。
    Java:

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class TestTwo extends Thread{
    	
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		System.out.println("请输入矩阵行列");
    		int line = sc.nextInt();
    		int colums = sc.nextInt();
    		
    		ArrayList<ArrayList<Integer>> arraylistline = new ArrayList<ArrayList<Integer>>();
    		for(int i = 0;i < line;i++) {
    			ArrayList<Integer> arraylistcolumns = new ArrayList<Integer>();
    			for(int j = 0;j < colums;j++)
    				arraylistcolumns.add(sc.nextInt());
    			arraylistline.add(arraylistcolumns);
    		}
    		
    		System.out.println("原矩阵");
    		for(int i = 0;i < line;i++) 
    			System.out.println(arraylistline.get(i));
    		
    		System.out.println("
    转置矩阵");
    		ArrayList<Integer> transposematrix = new ArrayList<Integer>();
    		for(int j = 0;j < colums;j++) {
    			for(int i = 0;i < line;i++) 
    				transposematrix.add(arraylistline.get(i).get(j));
    			System.out.println(transposematrix);
    			transposematrix.clear();
    		}
    		
    		sc.close();
    	}
    }
    /* Code Running Result
    请输入矩阵行列
    2 3
    1 2 3
    4 5 6
    原矩阵
    [1, 2, 3]
    [4, 5, 6]
    
    转置矩阵
    [1, 4]
    [2, 5]
    [3, 6]
    */
    

    接下来就要提现 Python 的强大了
    Python3:

    matrix = []
    print("请输入矩阵行列")
    line = int(input())
    colums = int(input())
    for x in range(line):
        row = []
        for y in range(colums):
            temp = int(input())
            row.append(temp)
        matrix.append(row)
    
    print("原矩阵")
    row = []
    for row in matrix:
        print(row)
    
    print("转置矩阵")
    row = []
    transposed = [[row[i] for row in matrix] for i in range(colums)]  # 矩阵转置
    for row in transposed:
        print(row)
    
    #Code Running Result
    # 2
    # 3
    # 1
    # 2
    # 3
    # 4
    # 5
    # 6
    # [1, 2, 3]
    # [4, 5, 6]
    # [1, 4]
    # [2, 5]
    # [3, 6]
    
    

    transposed = [[row[i] for row in matrix] for i in range(colums)]

    仅用了这一行就实现了矩阵的转置

  • 相关阅读:
    WPF Expander 炫酷自定义Style
    C#8.0 中的 【索引与范围】
    Windows的图形设备接口与Windows绘图
    第一个Windows窗口应用程序
    0-1背包问题的分枝—限界算法
    哈密尔顿回路(旅行售货员问题)的回溯算法
    背包问题的贪心算法
    实现矩阵连乘的动态规划算法
    用分治策略实现棋盘覆盖问题
    sql注入实例分析
  • 原文地址:https://www.cnblogs.com/jiaohuadehulike/p/14295024.html
Copyright © 2011-2022 走看看