我想访问矩阵的anti-diagonals作为视图。犰狳有可能吗?
人们希望看到我的代码
vec get_antidiag (const mat & A, const uint k) {
const uint n = A.n_rows;
const uint m = A.n_cols;
const uint start_row = max(0, static_cast<int>(k+1-m));
const uint end_row = min(k, n-1);
const uint len = end_row - start_row +1;
vec v (len);
for (uint i = 0; i < len; ++i) {
v(i) = A(i+start_row, k-(i+start_row));
}
return v;
}
void set_antidiag (mat & A, const uint k, const vec v) {
const uint n = A.n_rows;
const uint m = A.n_cols;
const uint start_row = max(0, static_cast<int>(k+1-m));
const uint end_row = min(k, n-1);
const uint len = end_row - start_row +1;
for (uint i = 0; i < len; ++i) {
A(i+start_row, k-(i+start_row)) = v(i);
}
}
答案 0 :(得分:0)
您可以使用diagvec()
和fliplr()
来提取诊断
vec get_antidiag (const mat & A, const uint k) {
mat B=A;
B = fliplr(B);
return diagvec(B,k);
}
没有找到任何设置诊断的东西......