Search code examples
c#.netarraysmatlabbinaryfiles

What is the equivalent of the Matlab reshape command in C#? How do I convert a C# 2D float array to a 3D float array?


In Matlab I have this

2darray = fread(fid, [160 304*304], 'float32');
3darray = reshape(2darray, [160 304 304]);

In C# I have this:

float[,] 2darray = new float[160, 304 * 304];

using (BinaryReader reader = new BinaryReader(File.OpenRead("path")))
{
    for(int i = 0; i < 160; i++)
    {
        for (int j = 0; j < 304 * 304; j++)
            2darray[i, j] = reader.ReadSingle();
    }
}

From here (in C#), how do I reshape the 2D float array into a 3D float array?


Solution

  • When you can't bring Mohammad to the mountain, bring the mountain to Mohammed.

            static void Main(string[] args)
            {
                float[,,] dArray = new float[160, 304, 304];
    
                using (BinaryReader reader = new BinaryReader(File.OpenRead("path")))
                {
                    for(int i = 0; i < 160; i++)
                    {
                        for (int j = 0; j < 304; j++)
                        {
                            for (int k = 0; k < 304; k++)
                            {
                                dArray[i, j, k] = reader.ReadSingle();
                            }
                        }
                    }
                }
            }