C#矩阵的简单操作

来源:互联网 发布:unity3d 人物模型真人 编辑:程序博客网 时间:2024/05/21 10:04
using System;using System.Collections.Generic;using System.Linq;using System.IO;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            string path = @"E://360壁纸//color.txt";            string outpath = @"F://Rout.txt";            string []words = File.ReadAllLines(path);//将文件中的每一行当做一个字符串存入字符串数组中,元素个数即行数              int row = words.Length;            int col = words[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length;            int[,] Matrix = new int[row, col];//定义一个二维数组用于存储矩阵            for (int i = 0; i < row; i++)            {                string[] s = words[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);                for (int j = 0; j < col; j++)                {                    Matrix[i, j] = Convert.ToInt32(s[j]);                }            }            StreamWriter sw = new StreamWriter(outpath, false);            for (int i = 0; i < col; i++)   //对矩阵进行转置输出到文件            {                for (int j = 0; j < row; j++)                {                    sw.Write(Matrix[j, i].ToString()+" ");                }                sw.WriteLine();            }            sw.Flush();            sw.Close();        }    }}

0 0
原创粉丝点击