[2.0] Elegant way to get a double[] row of a double[,] matrix.
Hi I am using a 2dimensional array:
Code:
double[,] matrix = new double[countX, countY];
I need to send every row of this array through a function with definition:
Code:
void ProcessRowOfMatrix(double[] row){ ... }
If I used a jagged [][] array instead of the [,] array this would be easy:
Code:
double[][] matrix = new double[countX][countY];
for(int i=0; i<countX; i++){
ProcessRowOfMatrix(matrix[i]);
}
With a [,] array I could do it like:
Code:
double[,] matrix = new double[countX,countY];
for(int i=0; i<countX; i++){
double[] tmpdblarr = new double[countY];
for(int j=0; j<countY; j++){
tmpdblarr[j] = matrix[i,j]
}
ProcessRowOfMatrix(tmpdblarr);
}
But that's not very elegant... In languages like mathlab rows or colums of 2dimensional arrays can be accesed like:
Code:
double[,] matrix = new double[countX, countY];
for(int i; i<countX;i++){
ProcessRowOfMatrix(matrix[i,:]);
}
This is very elegant! But it does not work in C# though.
I was wondering maybe there is just another syntax to achieve this also in C#? And maybe somebody can tell me what it is...
Thank you in advance
BramGo
Re: [2.0] Elegant way to get a double[] row of a double[,] matrix.
if ProcessRowOfMatrix() takes an array as parameters then you can use
Code:
double[,] matrix = new double[countX, countY];
for(int i; i<countX;i++){
ProcessRowOfMatrix(matrix[i]);
}
Re: [2.0] Elegant way to get a double[] row of a double[,] matrix.
I know what you're talking about in MatLab, but I'm not sure how they implement that. See, when the computer allocates memory for the array it can only allocate in one dimension. So it really creates countY + 1 single dimensional arrays (countY of them are countX long and one of them is countY long). Then it stores the pointers of countY of those arrays in the extra array. If I've explained that adequatly, then you can see why it is impossible for C# to give you a reference to an array where you can access all values in a given row.
ProcessRowOfMatrix could just take a reference to the whole matrix and an index to the row you want to process. ProcessRowOfMatrix can use a simple loop and there's no need for a temp array.
Re: [2.0] Elegant way to get a double[] row of a double[,] matrix.
Quote:
Originally Posted by ComputerJy
if ProcessRowOfMatrix() takes an array as parameters then you can use
Code:
double[,] matrix = new double[countX, countY];
for(int i; i<countX;i++){
ProcessRowOfMatrix(matrix[i]);
}
no no no! That's not true. It gives a syntax error...
Re: [2.0] Elegant way to get a double[] row of a double[,] matrix.
Quote:
Originally Posted by DNA7433
ProcessRowOfMatrix could just take a reference to the whole matrix and an index to the row you want to process. ProcessRowOfMatrix can use a simple loop and there's no need for a temp array.
... nope. I am allready using that Progress-function for something else. And I would like to reuse it.
So at the moment I use an additional function that looks like this:
Code:
private double[] GetRowFromMatrix(double[,] matrix, int rownr){
int length = matrix.GetLength(1);
double[] row = new double[length];
for (int i = 0; i < length; i++)
row[i] = matrix[rownr,i];
return row;
}
But the goal of my question was, kind of hoping there was a trick to do it with [i,*] or maybe [i,?] or with some Get function or something like that....
Re: [2.0] Elegant way to get a double[] row of a double[,] matrix.
Quote:
Originally Posted by DNA7433
I know what you're talking about in MatLab, but I'm not sure how they implement that. See, when the computer allocates memory for the array it can only allocate in one dimension. So it really creates countY + 1 single dimensional arrays (countY of them are countX long and one of them is countY long). Then it stores the pointers of countY of those arrays in the extra array. If I've explained that adequatly, then you can see why it is impossible for C# to give you a reference to an array where you can access all values in a given row.
However for me it would be enough just to be able to get rows, taking out columns is not necessary. That shouldn't be a problem, after all, isn't the [,] just stored the same way as a [][] behind the scene?
Re: [2.0] Elegant way to get a double[] row of a double[,] matrix.
sorry, I tried playing a little with your code
Code:
int countX = 0, countY = 0;
double[][] matrix = new double[countX][];
for (int i = 0; i < countX; i++)
{
matrix[countX] = new double[countY];
}
for (int i=0; i < countX; i++){
ProcessRowOfMatrix(matrix[i]);
}
}
void ProcessRowOfMatrix(double[] row)
{
}
I don't think this is much help but at least it works fine
Re: [2.0] Elegant way to get a double[] row of a double[,] matrix.
Sorry I misread your question, I thought you were asking for something like [:,i] instead of [i,:]. I don't think it's possible and I don't think under the hood double[][] is the same thing as double[,]. double[,] is one array with rank two and double[][] is an array of rank one whose references contain more arrays. In light of that I tried creating an array as such: double[][,] but could not find a syntax to instantiate it, however the datatype definition wasn't flagged as an error. Also, I couldn't find a way to isolate a specific row from a double[,] array. I think what you want is one of the purposes of using a jagged array.