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();
}
}
}
C#矩阵的简单操作
本文转载:CSDN博客