• 矩阵转置


    矩阵转置 - 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)]

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

  • 相关阅读:
    劝退人的不是算法本身,而是你以为是对的,但逻辑是对的,却报错
    小程序毕设开发笔记功能模块开发
    Python函数式编程
    Flask和Django的网络请求比较
    Flask与Django的表单类(待续)
    绍兴文理学院元培学院第十五届大学生程序设计竞赛题解
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password('123')' at line 1
    vim保存修改的只读文件
    树莓派连接显示屏后显示有黒边解决方法
    MySQL Error: : 'Access denied for user 'root'@'localhost'
  • 原文地址:https://www.cnblogs.com/jiaohuadehulike/p/14295024.html
Copyright © 2020-2023  润新知