How do I convert a arma::cx_mat to an array of arrays?
The motivation for the conversion is to use libmatio, which is a C library, to output a .mat file.
So far I have created a function to convert from arma:cx_mat to a vector of vectors:
std::vector<std::vector<double>> mat_to_vv(arma::cx_mat &M)
{
std::vector<std::vector<double>> vv(M.n_rows);
for(size_t i=0; i<M.n_rows; ++i)
{
vv[i] = arma::conv_to<std::vector<double>>::from(M.row(i));
};
return vv;
}
If you need convert real part from cx_mat into C array of arrays can use this function:
double** mat_to_carr(arma::cx_mat &M,std::size_t &n,std::size_t &m)
{
const std::size_t nrows = M.n_rows;
const std::size_t ncols = M.n_cols;
double **array = (double**)malloc(nrows * sizeof(double *));
for(std::size_t i = 0; i < nrows; i++)
{
array[i] = (double*)malloc(ncols * sizeof(double));
for (std::size_t j = 0; j < ncols; ++j)
array[i][j] = M(i + j*ncols).real();
}
n = nrows;
m = ncols;
return array;
}
Note, need free the array when it is no longer needed.
Example:
int main()
{
cx_mat X(5, 5, fill::randn);
std::size_t n,m;
auto array = mat_to_carr(X,n,m);
for (std::size_t i = 0; i < n; ++i)
{
for (std::size_t j = 0; j < m; ++j)
std::cout<<array[i][j]<<" ";
std::cout<<std::endl;
}
for(std::size_t i = 0; i < n; i++)
free(array[i]);
free(array);
return 0;
}
Related
So I got a function which creates me 2D array and fill it with test data.
Now I need to assign the pointer to an array
//Fill matrix with test data
int *testArrData(int m, int n){
int arr[n][m];
int* ptr;
ptr = &arr[0][0];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
*((ptr+i*n)+j) = rand()%10;
}
}
return (int *) arr;
}
int arr[m][n];
//Algorithm - transpose
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
arrT[j][i] = arr[i][j];
}
}
Is there any way of doing this?
There are at least four problems with the function.
//Fill matrix with test data
int *testArrData(int m, int n){
int arr[n][m];
int* ptr;
ptr = &arr[0][0];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
*((ptr+i*n)+j) = rand()%10;
}
}
return (int *) arr;
}
First of all you declared a variable length array
int arr[n][m];
Variable length arrays are not a standard C++ feature.
The second problem is that these for loops
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
*((ptr+i*n)+j) = rand()%10;
}
}
are incorrect. It seems you mean
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
*((ptr+i*m)+j) = rand()%10;
}
}
You are returning a pointer to a local array with automatic storage duration that will not be alive after exiting the function. So the returned pointer will be invalid.
And arrays do not have the assignment operator.
Instead use the vector std::vector<std::vector<int>>. For example
std::vector<std::vector<int>> testArrData(int m, int n){
std::vector<std::vector<int>> v( n, std::vector<int>( m ) );
for ( auto &row : v )
{
for ( auto &item : row )
{
item = rand() % 10;
}
}
return v;
}
This is how I would accomplish this. I agree with int ** because it is easy to understand if you dont know how to use vectors. Also, the rand() can cause trouble if you are using the result to index an array. Make sure to use abs(rand() % number) if you don't want negative numbers.
I've updated the answer due to some vital missing code.
// This method creates the overhead / an array of pointers for each matrix
typedef int* matrix_cells;
int **create_row_col_matrix(int num_rows, int num_cols, bool init_rnd)
{
num_rows = min(max(num_rows, 1), 1000); // ensure num_rows = 1 - 1000
num_cols = min(max(num_cols, 1), 1000); // ensure num_cols = 1 - 1000
int *matrix_total = new int[num_rows*num_cols];
// overhead: create an array that points to each row
int **martix_row_col = new matrix_cells[num_rows];
// initialize the row pointers
for (int a = 0; a < num_rows; ++a)
{
// initialize the array of row pointers
matrix_row_col[a] = &matrix_total[num_cols*a];
}
// assign the test data
if (init_rnd)
{
for (int run_y = 0; run_y < num_rows; ++run_y)
{
for (int run_x = 0; run_x < num_cols; ++run_x)
{
matrix_row_col[run_y][run_x] = abs(rand() % 10);
}
}
}
return matrix_row_col;
}
int src_x = 7, dst_x = 11;
int src_y = 11, dst_y = 7;
int **arr_src = create_row_col_matrix(src_y, src_x, true);
int **arr_dst = create_row_col_matrix(dst_y, dst_x, false);
for (int a = 0; a < dst_y; ++a)
{
for (int b = 0; b < dst_x; ++b)
{
arr_dst[a][b] = arr_src[b][a];
}
}
delete matrix_src[0]; // int *matrix_total = new int[src_y*src_x]
delete matrix_src; // int **matrix_row_col = new matrix_cell[src_y]
delete matrix_dst[0]; // int *matrix_total = new int[dst_y*dst_x]
delete matrix_dst; // int **matrix_row_col = new matrix_cell[dst_y]
// the overhead is matrix_src and matrix_dst which are arrays of row pointers
// the row pointers makes it convenient to address the cells as [rown][coln]
I have some problem with type casting.
Here is working code:
void init(signed char[16][16][16])
{
}
int main()
{
auto cache = new signed char[16][16][16];
init(cache);
return 0;
}
And here is a code with a problem if compiled in Windows. In Linux this code works just fine:
int main()
{
std::shared_ptr<signed char[16][16][16]> ptr;
ptr.reset(new signed char[16][16][16]);
init(ptr.get());
return 0;
}
MSVS gives me an error: Error C2664 'void init(signed char [][16][16])': cannot convert argument 1 from 'signed char (*)[16][16][16]' to 'signed char [][16][16]'
How to correctly use multidim arrays with shared_ptr? What is the type name of "auto cache"?
Here is an example with 3D shared_ptr
using myType = signed char;
using ManagedInt = std::shared_ptr<std::shared_ptr<std::shared_ptr<myType>>> ;
const int N = 16;
{
auto managedMat = ManagedInt();
managedMat.reset(new std::shared_ptr<std::shared_ptr<myType>>[N], [](auto p) {
for(uint32_t i = 0; i <N; ++i){
p->reset();
}
});
for(uint32_t i = 0; i< N;++i){
managedMat.get()[i].reset(new std::shared_ptr<myType>[N], [](auto p) {
for(uint32_t i = 0; i <N; ++i){
p->reset();
}
});
for(uint32_t j = 0; j < N; ++j){
managedMat.get()[i].get()[j].reset(new myType[N], [](auto p) {
delete[] p;
});
}
}
for(uint32_t i = 0; i < N; ++i)
for(uint32_t j = 0; j < N; ++j)
for(uint32_t k = 0; k < N; ++k)
managedMat.get()[i].get()[j].get()[k] = j;
for(uint32_t i = 0; i < N; ++i)
for(uint32_t j = 0; j < N; ++j)
for(uint32_t k = 0; k < N; ++k)
std::cout<<managedMat.get()[i].get()[j].get()[k];
}
Live
I recommend using vector though if you can
So i keep trying to transfer the elements but it keeps giving me repeated elements, it fails to properly copy the 2D array onto a 1D vector
// This was one of my attempts
vector<int> rando(int rowsize, int columnsize)
{
int elements = rowsize*columnsize;
vector<int> x(elements);
int matrix[100][100];
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
srand((int)time(0));
matrix[i][j]= -10 + rand() % 21;
for(int n=0; n < elements; n++)
x[n]=matrix[i][j];
}
// Ive also tried this
for(int n=0; n < elements; n++)
{
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
x[n]=matrix[i][j];
}
}
}
}
return x;
}
Why do you want to store data into the matrix first and copy it into the vector afterwards? Use the vector from the start.
std::vector<int> rando(std::size_t rowsize, std::size_t columnsize)
{
std::vector<int> v(rowsize*columnsize);
std::mt19937 mt{std::random_device{}()};
std::uniform_int_distribution<int> rand_dist(-10, 10);
for (auto & e : v) e = rand_dist(mt);
return v;
}
If you want to transfer data from a matrix into a vector you must calculate the proper index or just increment a single variable as Thomas Matthews suggests.
constexpr std::size_t n = 100, m = 100;
int matrix[n][m];
// do stuff with matrix
std::vector<int> v(n*m);
for (std::size_t i=0; i<n; ++i)
{
for (std::size_t j=0; j<m; ++j)
{
v[i*m + j] = matrix[i][j];
}
}
THe general copy should loop through the 2 dimensions, and just increment the target index at each iteration (no third nested loop):
int n=0;
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
...
x[n++]=matrix[i][j]; // not in an additional for loop !!
}
} // end of initialisation of matrix
If your matrix is a 2D array (i.e. contiguous elements) you can also take the following shortcut using <algorithm>:
copy (reinterpret_cast<int*>(matrix), reinterpret_cast<int*>(matrix)+elements, x.begin());
Try this:
unsigned int destination_index = 0;
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
x[destination_index++]=matrix[i][j];
}
}
The destination index is incremented after each assignment to a new slot.
No need for a 3rd loop.
It is enough to use two loops.
For example
srand((int)time(0));
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
matrix[i][j]= -10 + rand() % 21;
x[i * columnsize + j] = matrix[i][j];
}
}
In general if you have a two-dimensional array and want to copy nRows and nCols of each row elements in a vector then you can use standard algorithm std::copy declared in header <algorithm>
For example
auto it = x.begin();
for ( int i = 0; i < nRows; i++ )
{
it = std::copy( matrix[i], matrix[i] + nCols, it );
}
My code:
FILE * file;
file = fopen("c://catalog//file.txt", "r");
int m,n; //size of 2d array (m x n)
fscanf(file, "%d", &m);
fscanf(file, "%d", &n);
fclose(file);
printf("Size: %d x %d\n", m, n);
// create 2d array
char **TAB2 = new char*[m];
for (int i = 0; i < m; i++)
char *TAB2 = new char[n];
// display 2d array
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++)
{
printf("%c ", &TAB2[i][j]);
}
printf("\n");
}
How fill this array with chars or string? for example text = "someting", and for array 3x5 will be:
S o m e t
h i n g ?
? ? ? ? ?
I tried: TAB2[0][0] = 's'; *&TAB2[0][0] = 's'; for one char, and this does'nt work...
Probably I badly use pointers(?). Anyone help me?
The dynamic allocation array is wrong.
char **TAB2 = new char*[m];
for (int i = 0; i < m; ++i)
TAB2[i] = new char[n];
Check this link for help.
You could try this:
#include<iostream>
using namespace std;
int main() {
const int m = 3, n = 5;
char **TAB2 = new char*[m];
for (int i = 0; i < m; ++i)
TAB2[i] = new char[n];
char c;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
std::cin >> c;
TAB2[i][j] = c;
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
std::cout << TAB2[i][j];
}
std::cout << "\n";
}
// NEVER FORGET TO FREE YOUR DYNAMIC MEMORY
for(int i = 0; i < m; ++i)
delete [] TAB2[i];
delete [] TAB2;
return 0;
}
Output:
jorje
georg
klouv
jorje
georg
klouv
Important links:
How do I declare a 2d array in C++ using new?
How do I use arrays in C++?
The allocation of the array seems incorrect; it should be as follows.
char **TAB2 = new char*[m];
for (int i = 0; i < m; i++)
TAB2[i] = new char[n];
There is a simple function creating an zero-filled matrix stored in array.
void zeroMatrix(const int rows, const int columns, void* M)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
*(((double *)M) + (rows * i) + j) = 0;
}
How do I change the code to use std::unique_ptr<double> as M?
Since there is no ownership transfer to the zeroMatrix function, what you need is a reference:
(Assuming M is a vector)
void zeroMatrix(const int rows, const int columns, std::vector<double> &M)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < columns; j++)
M[(rows * i) + j] = 0;
}