フィルターのクリア

How to extract a vector from a matrix with indices?

8 ビュー (過去 30 日間)
Mr M.
Mr M. 2018 年 4 月 18 日
回答済み: Star Strider 2018 年 4 月 18 日
I have for example an 3x2 matrix M. And I need the first element from the first row, second element from the second row, and first element from the third row. I have column indices in a vector [1 2 1], how to extract the vector from M?

回答 (1 件)

Star Strider
Star Strider 2018 年 4 月 18 日

Create a linear index with sub2ind, and use it to get the elements you want:

M = randi(99, 3, 2)                                             % Create Matrix
linidx = sub2ind(size(M), 1:size(M,1), [1 2 1])                 % Linear Index
Out = M(linidx)
M =
    95    15
    49    42
    80    91
Out =
    95    42    80

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by