#How to insert an element to Eigen::ArrayXd?

8 messages · Page 1 of 1 (latest)

frail reef
#

I tried to look into the official docs but have little experience reading them. It is a dynamic array so I assume it should have push_back method like std::vector?

stray kindleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

woven falcon
#

it's not designed with dynamic resizing in mind

#

if you need to add an element you'll have to manually resize the array and copy the existing elements, plus the new one, into it

#

like

Eigen::ArrayXd arr(3);
arr << 1, 2, 3;

double newElement = 4.0;

Eigen::ArrayXD newArr(arr.size() + 1);

newArr.head(arr.size()) = arr;

newArr(arr.size()) = newElement;

std::cout << "New array: \n" < newArr < std::endl;
frail reef
stray kindleBOT
#

@frail reef Has your question been resolved? If so, run !solved :)

woven falcon
#

you'll get better at reading docs over time, but not all docs are created equal - there wouldn't be stuff on stackoverflow if no one else needed help beyond the official documentation. regarding resize: yes, you can also use resize to change the size of Eigen::ArrayXd, but you'll still need to manually copy over the old data and insert the new element, as above