# \[AMPL 24523\] 2d variable into c++ matrix

**URL:** https://discuss.ampl.com/t/ampl-24523-2d-variable-into-c-matrix/367
**Category:** Google Group Mirror
**Created:** [February 9, 2023, 4:21pm UTC](https://discuss.ampl.com/t/ampl-24523-2d-variable-into-c-matrix/367 "2023-02-09T16:21:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Samuele\_Viaro](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/samuele_viaro/32/178_2.png) [@Samuele\_Viaro](https://discuss.ampl.com/u/Samuele_Viaro)
#### Post date: [February 9, 2023, 4:21pm UTC](https://discuss.ampl.com/t/ampl-24523-2d-variable-into-c-matrix/367/1 "2023-02-09T16:21:03Z")

</div>

Hello,

I have a variable of the form t[j,i]. I would like to save these values into a 2D vector in C++…  
I believe I should use the dataframe but I am a bit confuse on how to do it…  
Is it possible? Anyone who knows how to do this?  
Thank you!  
Sam

---

<div class="post-metadata">

### Author: ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)
#### Post date: [February 9, 2023, 6:20pm UTC](https://discuss.ampl.com/t/ampl-24523-2d-variable-into-c-matrix/367/2 "2023-02-09T18:20:09Z")

</div>

Hi Sam,

You can retrieve the values into a DataFrame with: ampl::DafaFrame df = ampl.getVariable(“t”).getValues();

After that you can use a function such as the following to convert the DataFrame into a vector of vectors.

std::vector\<std::vector\> dataframe\_to\_vector(const ampl::DataFrame &df)  
{  
std::vector\<std::vector\> data;  
int nrows = df.getNumRows();  
int ncols = df.getNumCols();  
data.reserve(nrows);  
for (auto it = df.begin(); it != df.end(); it++)  
{  
data.push\_back(std::vector(ncols));  
for (int i = 0; i \< ncols; i++)  
{  
data.back()[i] = (\*it)[i].dbl();  
}  
}  
return data;  
}

For this type of questions asking on our new forum ([https://discuss.ampl.com/](https://discuss.ampl.com/)) is better since it works a lot better with code.

---

<div class="post-metadata">

### Author: ![Samuele\_Viaro](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/samuele_viaro/32/178_2.png) [@Samuele\_Viaro](https://discuss.ampl.com/u/Samuele_Viaro)
#### Post date: [February 10, 2023, 10:16am UTC](https://discuss.ampl.com/t/ampl-24523-2d-variable-into-c-matrix/367/3 "2023-02-10T10:16:16Z")

</div>

Thank you Filipe,

it doesn’t save the values as I expected though…maybe because in my variable t the number of columns depends on the number of rows through the parameter A??

_var t{j in J, i in I: i\<= A[j] } \>= 0;_

do I need to specify the value of A when I save the dataframe??  
I apologize if I did not mention this sooner…  
Thanks  
Sam

---

<div class="post-metadata">

### Author: ![AMPL\_Google\_Group](https://avatars.discourse-cdn.com/v4/letter/a/9dc877/32.png) [@AMPL\_Google\_Group](https://discuss.ampl.com/u/AMPL_Google_Group)
#### Post date: [February 13, 2023, 1:06pm UTC](https://discuss.ampl.com/t/ampl-24523-2d-variable-into-c-matrix/367/4 "2023-02-13T13:06:57Z")

</div>

Hi Sam,

The DataFrame should have three columns, one of index “i”, one for index “j”, and another for “t[i,j]”. Do you want to load this values into a matrix? You can do that as follows:

for (auto it = df.begin(); it != df.end(); it++)  
{  
int i = (int) (\*it)[0].dbl();  
int j = (int) (\*it)[1].dbl();  
double value = (\*it)[2].dbl();  
matrix[i][j] = value;  
}
