五维思考

学习要加,骄傲要减,机会要乘,懒惰要除。 http://www.5dthink.cn

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
class Program
{
    static void Main(string[] args)
    {
        double[] a = { 1, 2, 3, 4, 5, 6 };
        double[,] b = Row2VecD(a);
        double[,] c = Row2ArrT(a, 2);
        //double[,] c = RowToArrD(a, 2);
        double[] d = Arr2RowD(c);

        Console.WriteLine("\n行向量→列向量");
        for(int i = 0; i < b.GetLength(0); i++)
        {
            for(int j = 0; j < b.GetLength(1); j++)
            {
                Console.WriteLine("b[{0},{1}]:{2}", i, j, b[i, j]);
            }
        }

        Console.WriteLine("\n\n行向量→数组");
        for (int i = 0; i < c.GetLength(0); i++)
        {
            for (int j = 0; j < c.GetLength(1); j++)
            {
                Console.WriteLine("c[{0},{1}]:{2}", i, j, c[i, j]);
            }
        }

        Console.WriteLine("\n\n数组→行向量");
        for (int i = 0; i < d.Length; i++)
        {
            Console.WriteLine("d[{0}]:{1}", i, d[i]);
        }

        Console.ReadKey();
    }

    //行向量→数组(内存复制版)
    static double[,] RowToArrD(double[]src,int row)
    {
        if (src.Length % row != 0) return null;
        int col = src.Length / row;
        double[,] dst = new double[row, col];
        for(int i = 0; i < row; i++)
        {
            //说明:“二维数组”【顺序储存】<=>“一维数组”
            Buffer.BlockCopy(src, i * col * sizeof(double), dst, i * col * sizeof(double), col * sizeof(double));
        }
        return dst;
    }

    //行向量→列向量(内存复制版)
    static double[,] Row2VecD(double[] src)
    {
        double[,] dst = new double[src.Length, 1];
        Buffer.BlockCopy(src, 0, dst, 0, sizeof(double) * src.Length);
        return dst;
    }

    //行向量→数组(循环版;泛型版)
    static T[,] Row2ArrT<T>(T[] vec, int row)
    {
        if (vec.Length % row != 0) return null;
        int col = vec.Length / row;
        T[,] ret = new T[row, col];
        for (int i = 0; i < vec.Length; i++)
        {
            ret[i / col, i % col] = vec[i];
        }
        return ret;
    }
    
    //数组→行向量(内存复制版)
    static double[] Arr2RowD(double[,] src)
    {
        int elem = src.GetLength(0) * src.GetLength(1);
        double[] dst = new double[elem];
        Buffer.BlockCopy(src, 0, dst, 0, elem * sizeof(double));
        return dst;
    }
}
posted on 2022-04-28 13:36  五维思考  阅读(1200)  评论(0编辑  收藏  举报

QQ群:1. 全栈码农【346906288】2. VBA/VSTO【2660245】