zoukankan      html  css  js  c++  java
  • SWM格式稀疏权重矩阵转换为方阵形式全过程分享

    在进行空间统计实验过程中,经常涉及到空间权重矩阵的处理,有时候需要将ArcGIS生成的swm格式的权重矩阵转换为形如“0 1”的方阵格式。这里将我的办法整理出来。

    1.用如下工具箱生成swm格式的权重矩阵

      

    2.将swm格式的权重矩阵转换为dbf属性表

        

    3.用excel打开dbf将其转换为txt文本文件

    4.写程序转换格式并保存

    代码如下:

     1 static void Main(string[] args)
     2         {
     3             //读取文件并转换格式
     4             StreamReader sr = File.OpenText("E:\AcaDissertation\Data\weight_arc.txt");
     5             double[,] weights = new double[47, 47];
     6             while (sr.ReadLine() != null)
     7             {
     8                 string[] line = sr.ReadLine().Split('	');
     9 
    10                 weights[int.Parse(line[0])-1, int.Parse(line[1])-1] = double.Parse(line[2]);
    11             }
    12             SaveMatrix(weights, "E:\AcaDissertation\Data\weight_mat.txt");
    13             Console.WriteLine("好了!");
    14         }
    15 
    16         // 保存矩阵到文件
    17         public static void SaveMatrix(double[,] InMatrix, string OutFileName)
    18         {
    19             int row = InMatrix.GetLength(0), col = InMatrix.GetLength(1);
    20             FileStream aFile = new FileStream(OutFileName, FileMode.OpenOrCreate);
    21             StreamWriter sw = new StreamWriter(aFile);
    22             for (int i = 0; i < row; i++)
    23             {
    24                 for (int j = 0; j < col; j++)
    25                 {
    26                     sw.Write("{0}{1}", InMatrix[i, j], " ");
    27                 }
    28                 sw.WriteLine();
    29             }
    30             sw.Close();
    31         }

    最后的最后,转换后的权重矩阵为:

  • 相关阅读:
    [LintCode] 最长上升子序列
    [LintCode] 最长公共前缀
    [LintCode] A + B 问题
    [hihoCoder] 拓扑排序·一
    [LintCode] 拓扑排序
    [LintCode] 第k大元素
    [LintCode] 最小路径和
    [LeetCode] Factorial Trailing Zeros
    [LintCode] 尾部的零
    [LeetCode] Length of Last Word
  • 原文地址:https://www.cnblogs.com/yif1991/p/5241663.html
Copyright © 2011-2022 走看看