I'm trying to rewrite code I've written in matlab in C++ instead.
I have a long cell in matlab containing 256 terms where every term is a 2x2 matrix. In matlab i wrote it like this.
xA = cell(1,256);
xA{1}=[0 0;3 1];
xA{2}=[0 0;13 1];
xA{3}=[0 0;3 2];
and so on...
What would be the easiest thing to use in c++?
开发者_开发技巧Can I give an array with the dimensions [256][2][2] all the 4 values at a time or do I have to write one line for every specific valuen in the array?
/Mr.Buxley
You can certainly initialize them all at once, although it sounds like a lot of tedious typing:
float terms[256][4] = {
{ 0, 0, 3, 1 },
{ 0, 0, 13, 1 },
{ 0, 0, 3, 2}
...
};
I simplified it down to an array of 256 4-element arrays, for simplicity. If you wanted to really express the intended nesting, which of course is nice, you need to do:
float terms[256][2][2] = {
{ { 0, 0 }, { 3, 1 } },
{ { 0, 0 }, { 13, 1 } },
{ { 0, 0 }, { 3, 2 }}
...
};
That would be 256 lines, each of which has a "list" of two "lists" of floats. Each list needs braces. But, since C++ supports suppressing braces in things like these, you could also do:
float terms[256][2][2] = {
{ 0, 0, 3, 1 },
{ 0, 0, 13, 1 },
{ 0, 0, 3, 2}
...
};
In fact, you could even remove the braces on each line, they're optional too. I consider relying on the suppression slightly shady, though.
If you want to use higher-level types than a nested array of float
(such as a Matrix<2,2>
type, or something), initialization might become tricker.
精彩评论