Is there an easier way to construct a 5 Diagonal matrix in Eigen? I can probably run for loops and allocate diagonals and zeros, but I did come across Diagonal<> just not sure how to use it for 5 diagonals, instead of one. Any ideas? EDIT: Figured this one out! For those wondering; you can use
matrix.diagonal(+n) = vector;
matrix.diagonal(-n) = vector;
to access super/sub diagonals of a matrix and write over them with vectors.
General side question: Is there a way I can skip an allocation when running a for loop in C++? For example:
int n; //size of matrix
MatrixXd m(n,n); //nxn matrix
for(int i=0; i<n; i++)
{
m(i,i) = 5;
m(i,i+1) = 6;
m(i,i-1) = 4;
m(i,i+2) = 7;
m(i,i-2) = 3;
}
for (int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(m(i,j) = something) //I want the loop to skip m(i,j) where
break; //i have already allocated values to m(i,j)
//How do I do that, in general, in C++?
else
{ m(i,j) = 0;}
}
}
Thanks
答案 0 :(得分:1)
听起来好像您想跳过对角线,因为它们已被初始化(此处分配的术语不正确)。
查看设置对角线的循环,您会看到所设置的每个(i,j)都遵循abs(i-j) <= 2
。例如,当您设置元素(i, i+2) -> abs(i-(i+2)) -> abs(-2) -> 2
小于或等于2时。
因此,第二个循环中的条件应为:
if (abs(i-j) <= 2)
continue;//break will exit the loop, continue will skip to the next iteration