select values from matrix

1 回表示 (過去 30 日間)
michael
michael 2021 年 1 月 12 日
回答済み: Fangjun Jiang 2021 年 1 月 12 日
Hi,
I have a 2D matrix of size 2x360 and a vector of length 360 which contains values 1 or 2.
From the matrix, I'd like to select all the 360 values from selected row as defined in the vector.
How can I do that?
For example:
A= [ 1 2 3 4 5;
6 7 8 9 10]
idx = [1 2 1 2 1]
The result shall be [1 7 3 9 5]

採用された回答

Fangjun Jiang
Fangjun Jiang 2021 年 1 月 12 日
loc=sub2ind(size(A),idx,1:size(A,2));
>> out=A(loc)
out =
1 7 3 9 5

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by