Division of two multidimensional arrays

mobsaleh

New member
Joined
Nov 16, 2021
Messages
4
Programming Experience
Beginner
Hi Guys,
I'm coming from MATLAB background & I'm trying to learn C#.
In my program, I'm at the point where I need to divide two arrays.
My understanding is arrays are multiplied/divided according to matrix rules.
I'm multiplying (225x225) X (225,1). I'm expecting an answer with (225,1)!
However, P = A\R isn't working & is throwing 3 errors.
My question is: Is right division a thing in C#?
If yes, why isn't it working? Am I missing something?
If no, how do I go about inverting matrix A in C#?

C#:
int Imax = 15;
Int Jmax = 15;
double[,] A = new double[Imax * Jmax, Imax * Jmax];   // Gives 225x225 square matrix
double[,] R = new double[Imax * Jmax, 1];              // Gives 225x1 matrix

// The equation governming the problem is: A x P = R
// I have to calculate P by doing by multiplying by invers (or right division)

Pressure = A\R;
 
Unfortunately, you are conflating 2 dimensional arrays. Although a 2 dimensional array can be use to represent a matrix (from the programmers point of view), there are no 2 dimensional array operators that will treat them like matrices and actually perform a matrix like operation.

You will need to use a real Matrix class that has the operators that you need to get a MatLab-like C# code.
 
Also C# does not have a \ division operator. Only a / operator.
 
Also C# does not have a \ division operator. Only a / operator.
If I remember correctly, division of two integral values performs an integer division while if one or more floating-point values are involved then you get a floating-point result. You would have to call Math.Floor to get an integral value from that floating-point result to get the result of an integer division. I'm assuming that, like VB, MatLab uses \ for integer division.
 
I never did VB or MatLab, so I am unfamiliar with their operators.
 
Apparently the the \ operator in MatLab is for:
Solve systems of linear equations Ax = B for x
according to the documenation.

Based on other documentation in the same site, to do right array division in MatLab you are supposed to use ./, or use / to do a right matrix division.
 
Last edited:
Looking at the OP, I should have realised that \ wasn't integer division, given that it's being used on two 2D arrays. I guess I just went by the mention of the / operator.
 
As I recall, the Matrix that comes with the .NET Framework is more geared towards graphics work and so it only has multiplication and inversion as it's more complicated operations. Math.NET's Matrix class might provide the operations that you need.

 
Hey Guys, thanks for your input. Seems like C# isn't designed to do complex math operations.
I ended up writing a method for matrix division & it worked just fine.
 
Back
Top Bottom