Extract variable rows and columns from a matrix

2 次查看(过去 30 天)
I wish to input a variable row and column to extract a row length vector from a matrix at each update.
A small example input matrix is:
A = 0.0975 0.9649 0.4854 0.9157 0.0357 0.7577
0.2785 0.1576 0.8003 0.7922 0.8491 0.7431
0.5469 0.9706 0.1419 0.9595 0.9340 0.3922
0.9575 0.9572 0.4218 0.6557 0.6787 0.6555
I wish to extract a row variable, where row length is the number of column(6) of A and the row may range from 1 to 4.
An example follows:
A(row) = A([2 4 3 2 1 3]) = [0.2785 0.9572 0.1419 0.7922 0.0357 0.3922].
Although I can brute force it with a for loop, that is probably not efficient for a matrix column size in the hundreds.
[nr,nc] = size(A);row = [2 4 3 2 1 3];
for i = 1:nc
extract_vec(i) = A(row(i),i);
end
I prefer a method that takes advantage of matlab's matrix operations.

采纳的回答

madhan ravi
madhan ravi 2020-7-28
A(sub2ind([nr, nc], rows, 1:nc))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by