Access to array elements
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a matrix, A, and I want to access the elements given by the vectors ind_rows, ind_cols. If I make A(ind_rows,ind_cols) I get a matrix of size ind_rows times ind_cols, but I am just interested in the diagonal of this matrix!
I have found a way to do this:
siz = size(A);
AA = A(1:end); % convert the matrix in a vector
IND = sub2ind(siz,ind_rows,ind_cols);
res = AA(IND);
It works, but I guess there must be a way to do this without rearranging the matrix in a vector, and in a cleaner way.
Thanks in advance!
Ana
0 个评论
采纳的回答
更多回答(1 个)
Image Analyst
2011-11-14
Ana: The most common way to get the diagonals is to use the eye() function:
m_DiagonalsOnly = m(logical(eye(size(m, 1))))
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!