Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting jagged arrays to 2-dim IArray #25

Open
mgirardis opened this issue Mar 19, 2024 · 0 comments
Open

Converting jagged arrays to 2-dim IArray #25

mgirardis opened this issue Mar 19, 2024 · 0 comments

Comments

@mgirardis
Copy link

mgirardis commented Mar 19, 2024

I couldn't find a MatFileHandler native way to handle conversion of 2-dimensional jagged arrays into an IArray representing a MATLAB matrix, so I coded this simple extension method that could be added/enhanced/adapted to native MatFileHandler.

It can also be adapted for inputs of type T[,]

public static class MyExtensionMethods
{
    public static IArrayOf<T> NewArray<T>(this DataBuilder matDataBuilder, T[][] data, params int[] dimensions)
             where T : struct
        {
            var array = matDataBuilder.NewArray<T>(dimensions);
            var m = data.Length;
            var n = data[0].Length; // assuming the jagged array represents a 2d matrix; it could be checked though
            for (var i = 0; i < m; i++)
            {
                for (var j = 0; j < n; j++)
                {
                    array[j * m + i] = data[i][j];
                }
            }
            return array;
        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant