return matrix rows and columns using a one-to-one mapped vector for rows and vector for column

I have a large NxN matrix A. How can I use a column vector pnRowsToSelect = [row1; row2; ...; rowM] and a column vector pnColsToSelect = [col1;col2;...;colM] to return A(row1,col1), A(row2, col2), ..., A(rowM, colM) when row1, row2, ..., rowM can be any rows (not necessarily consecutive) - same for columns. That is "pnRowsToSelect" is the rows whose corresponding columns are "pnColsToSelect" such that the i-th row of "pnRowsToSelect" and i-th row of "pnColsToSelect" is the i-th element of A that I want to return.
For instance, "A= rand(10000);" and "pnRowsToSelect= [ 1; 1000; 4];" and "pnColsToSelect = [ 10 5 100];", then I want to have A(1,10), A(1000, 5), and A(4, 100). I tried "A(pnRowsToSelect, pnColsToSelect)" and this returns A(1,10) , A(1,5), A(1,100) ; A(1000, 10), A(1000, 5), A(1000,100) ; A(4, 10), A(4,5), A(4,100)] whose diagonal element is what I'm looking for. Therefore, I can use "diag(A(pnRowsToSelect, pnColsToSelect))". Is there a more efficientway to do this?

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by