String数据转Matrix矩阵

String数据转Matrix矩阵

private Matrix String_To_Matrix(string str)

{

            int[] Remove_Num = new int[10];

            int Remove_Int = 0;

            string Str_One = str;

            //{ {M11:1 M12:0 M13:0 M14:0} {M21:0 M22:1 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:0 M42:0 M43:0 M44:1} }

            //string Str_One = "{ {M11:1 M12:0 M13:0 M14:0} {M21:0 M22:1 M23:0 M24:0} {M31:0 M32:0 M33:1 M34:0} {M41:0 M42:0 M43:0 M44:1} }";

            for (int i = 0; i < Str_One.Length; i++)

            {

                //string str = Str_One[i].ToString ();

                if (Str_One[i].Equals('{') || Str_One[i].Equals('}'))

                {

                    Remove_Num[Remove_Int] = i;

                    Remove_Int += 1;

                }

            }

            Remove_Int = 0;

            int Remove_num_int = 0;

            string Str_Two = "";// M11:1 M12:0 M13:0 M14:0 M21:0 M22:1 M23:0 M24:0 M31:0 M32:0 M33:1 M34:0 M41:0 M42:0 M43:0 M44:1

            foreach (var item in Remove_Num)

            {

                //string str = item.ToString();

                Remove_num_int = item - Remove_Int;

                Str_Two = Str_One.Remove(Remove_num_int, 1);

                Str_One = Str_Two;

                Remove_Int += 1;

            }

            string[] M_int = new string[16];// 1 0 0 0 ,0 1 0 0 ,0 0 1 0 ,0 0 0 1

            for (int i = 0; i < 16; i++)

            {

                M_int[i] = (Str_Two.Split(' ')[i + 1]).Split(':')[1];

            }

            //Matrix aaaa = new Matrix(0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1);

            Matrix new_Matrix = new Matrix(float.Parse(M_int[0]), float.Parse(M_int[1]), float.Parse(M_int[2]), float.Parse(M_int[3]), float.Parse(M_int[4]), float.Parse(M_int[5]), float.Parse(M_int[6]), float.Parse(M_int[7]), float.Parse(M_int[8]), float.Parse(M_int[9]), float.Parse(M_int[10]), float.Parse(M_int[11]), float.Parse(M_int[12]), float.Parse(M_int[13]), float.Parse(M_int[14]), float.Parse(M_int[15]));

 

            return new_Matrix;

}

注:取Matrix格式string字符串,循环判断添加索引int[]数组,去除‘{}',再根据’  ‘进行对整体string分割取string[],之后依据':'分割读取float于float[]数组,对新建Matrix依次赋值,达到string转matrix效果.....

转载于:https://www.cnblogs.com/XiaoLang0/p/10214056.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值