Reputation: 2711
I have a 2D array that I need to process one column at a time. I wrote a sample code to illustrate what I want to do. It will obviously not compile.
float a[3][16]; // Create 2D array
void function1() // This function will be called from my application
{
for (int i=0; i<16; i++) // For each column of the 2D array "a"
{
// Call this function that only take 1D array parameters
function2(a[][i]); // What I want is all rows in column i
// MATLAB syntax is: function2(a(:,i));
}
}
void function2(float b[])
{
// Something
}
I know I can make a temporary array, save each columns into that and use it as parameter in function2. I would like to know if there is a better method or how you would do this?
Upvotes: 1
Views: 79
Reputation: 5301
The best way would be to pass the entire 2d array to function2() along with a parameter that selects the column. Then just iterate along the axis.
for (int i=0; i<16; i++) // For each column of the 2D array "a"
{
function2( a , i );
}
void function2(float b[Y][X] , size_t col )
{
for( size_t i = 0 ; i < Y ; i++ )
b[i][col] = ...
}
Upvotes: 4