Exemplo n.º 1
0
 public static extern SafeArrayPtr mxCreateNumericMatrix(int m, int n, mxNumericType classId, mxComplexity complexFlag);
Exemplo n.º 2
0
        public static Array ToArray(byte[] buffer, int m, int n, mxNumericType numType)
        {
            if (m < 1 || n < 1)
                throw new ArgumentOutOfRangeException("The dimension must be larger than 1.");

            switch (numType) {
                case mxNumericType.UINT8:{
                        if (buffer.Length != m * n * byte_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        byte[,] values = new byte[m, n];

                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                values[r, c] = buffer[c * m + r];
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT8: {
                        if (buffer.Length != m * n * byte_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        sbyte[,] values = new sbyte[m, n];

                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                values[r, c] = (sbyte)buffer[c * m + r];
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT16: {
                        if (buffer.Length != m * n * short_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        short[,] values = new short[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * short_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * short_size, short_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.UINT16: {
                        if (buffer.Length != m * n * ushort_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        ushort[,] values = new ushort[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * ushort_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * ushort_size, ushort_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT32: {
                        if (buffer.Length != m * n * int_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        int[,] values = new int[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * int_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * int_size, int_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.UINT32:{
                        if (buffer.Length != m * n * uint_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        uint[,] values = new uint[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * uint_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * uint_size, uint_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.INT64: {
                        if (buffer.Length != m * n * long_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        long[,] values = new long[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * long_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * long_size, long_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.UINT64: {
                        if (buffer.Length != m * n * ulong_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        ulong[,] values = new ulong[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * ulong_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * ulong_size, ulong_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.SINGLE: {
                        if (buffer.Length != m * n * float_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        float[,] values = new float[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * float_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * float_size, float_size);
                            }
                        }

                        return values;
                    }
                case mxNumericType.DOUBLE: {
                        if (buffer.Length != m * n * double_size)
                            throw new ArgumentException("The buffer length is not match with m*n.");

                        double[,] values = new double[m, n];
                        int offset;
                        for (int r = 0; r < m; r++) {
                            for (int c = 0; c < n; c++) {
                                offset = (c * m + r) * double_size;
                                Buffer.BlockCopy(buffer, offset, values, (r * n + c) * double_size, double_size);
                            }
                        }

                        return values;
                    }
                default:
                    throw new ArgumentException();
            }
        }
Exemplo n.º 3
0
 public static extern SafeArrayPtr mxCreateNumericArray(int ndim, int[] dims, mxNumericType classId, mxComplexity complexFlag);