How do i get the values of a 2D matrix based on column indices ?

2 ビュー (過去 30 日間)
YI CHONG ONG
YI CHONG ONG 2020 年 9 月 23 日
コメント済み: YI CHONG ONG 2020 年 9 月 23 日
I know this is a simple question, but I cant seem to find answers on this.
Given a 2D matrix of any size and a vector of indices, I want to index the elements in the matrix based on the column indices values in order.
Eg.
matrix = [1 2 3
4 5 6
7 8 9] column indices = [1,3,1]
Output = [1,6,7] -> column index 1 = 1, column index 3 = 6, column index 1 = 7

採用された回答

madhan ravi
madhan ravi 2020 年 9 月 23 日
編集済み: madhan ravi 2020 年 9 月 23 日
Output = matrix(sub2ind(size(matrix), 1 : size(matrix, 1), column_indices))

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 9 月 23 日
Use sub2ind()
matrix = [1 2 3
4 5 6
7 8 9];
cols = [1 3 1];
idx = sub2ind(size(matrix), 1:size(matrix, 1), cols);
matrix(idx)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by