How can i get each coloumn of a 2D array as 1D arrays??

R

Renjini

hi everyone,
i have a problem. i have been using an FFT method for 1D arrays. now i
need to extend the code for 2D arrays..i thought it would be simple and
i managed to implement the FFT method for every row in the array..the
problem is now that i need to do the same for each of the
coloumns...and the elements shud not be copied into any other
arrays..because the FFT is to be applied on the original array...and
the same must be given as output..can anyone tell me how i could get
the coloumns each as a 1D array...?? i am including the int main()
here.
i'd be so thankful for the help.

int main(int argc, char *argv[])
{

double x1[][]={{2,3,4,5},{4,-1,2,3},{1,2,3,4}};
double y1[][]={{-1,0,2,0},{1,2,2,1},{0,0,0,0}};

cout << "\nInput: \n\n";
for (int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cout << x1[j] << "+" << y1[j] <<"i \t";
}
cout<<"\n";
}


for(int i=0;i<3;i++)
{
// FFT method for each row

FFT(-1,2,x1,y1);
}

/* for (int i=0;i<(sizeof(x1)/sizeof(x1[0]));i++)
cout << "Output: " << x1 << " + i " << y1 <<"\n";

//cout << x1[1] <<"\n";*/
cout<<"\nOuput: \n\n";
for (int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cout << x1[j] << "+" << y1[j] <<"i \t";
}
cout<<"\n";
}

getch();
return 0;

}
 
B

Ben Radford

Renjini said:
hi everyone,
i have a problem. i have been using an FFT method for 1D arrays. now i
need to extend the code for 2D arrays..i thought it would be simple and
i managed to implement the FFT method for every row in the array..the
problem is now that i need to do the same for each of the
coloumns...and the elements shud not be copied into any other
arrays..because the FFT is to be applied on the original array...and
the same must be given as output..can anyone tell me how i could get
the coloumns each as a 1D array...?? i am including the int main()
here.
i'd be so thankful for the help.

int main(int argc, char *argv[])
{

double x1[][]={{2,3,4,5},{4,-1,2,3},{1,2,3,4}};
double y1[][]={{-1,0,2,0},{1,2,2,1},{0,0,0,0}};

cout << "\nInput: \n\n";
for (int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cout << x1[j] << "+" << y1[j] <<"i \t";
}
cout<<"\n";
}


for(int i=0;i<3;i++)
{
// FFT method for each row

FFT(-1,2,x1,y1);
}

/* for (int i=0;i<(sizeof(x1)/sizeof(x1[0]));i++)
cout << "Output: " << x1 << " + i " << y1 <<"\n";

//cout << x1[1] <<"\n";*/
cout<<"\nOuput: \n\n";
for (int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cout << x1[j] << "+" << y1[j] <<"i \t";
}
cout<<"\n";
}

getch();
return 0;

}


The following class might be useful to you:

//-----------------------------------------------------------------------------

#include <iostream>

template <typename T>
class ColArray {
public:
template <int N, int M>
ColArray(T (&arr)[N][M], int col) : _p(*arr + col), _sz(M) {}

T& operator[](int index) { return *(_p + index * _sz); }
const T& operator[](int index) const { return *(_p + index *
_sz); }

operator T*() { return _p; }

private:
T* _p;
int _sz;
};

int main()
{
int arr[3][3] = {{11, 12, 13},
{21, 22, 23},
{31, 32, 33}};

// Create a new 1D 'array' by passing the original 2D
// array and the column you want (counting from 0).
ColArray<int> col(arr, 2);

for (int i = 0; i < 3; i++)
std::cout << col << std::endl;

return 0;
}

// Output:
// $ ./cols
// 11
// 21
// 31

//-----------------------------------------------------------------------------

I just wrote it now - it automates the task of adding the row size onto
the address of the first element in the column to reach the desired
index in the same column. Unfortunately I there's no way of doing this
more efficiently (that I can see of) given the constraints you
specified. You are going to take a hit calculating either the row or
column FFT because of the way arrays are ordered.

Anyway you should be able to pretend that a ColArray object is normal
array and pass it to your FFT function. If I've missed anything you can
add it to the class yourself - subscripting and array to pointer
decomposition was all I could think of off the top of my head.

To calculate the column FFT:

for (int i = 0; i < 3; i++) {
// FFT method for each row
FFT(-1, 2, ColArray(x1, i), ColArray(y1, i));
}

Hope that helps =)
 
B

Ben Radford

Ben said:
// Output:
// $ ./cols
// 11
// 21
// 31

I just realised that I posted the output for the first column. This
should be:

// $ ./cols
// 13
// 23
// 33

I hope there's no more mistakes in there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top