Selecting an element from each column of a matrix using another matrix

2 ビュー (過去 30 日間)
Charles Brown-King
Charles Brown-King 2016 年 2 月 4 日
回答済み: Azzi Abdelmalek 2016 年 2 月 4 日
I have a 130x87 matrix (A) and I need to select an element from each column based on the numbers in a 1x87 matrix (B). So if the first number in matrix B is 12, I need the 12th number in the first column of matrix A.

採用された回答

Stephen23
Stephen23 2016 年 2 月 4 日
編集済み: Stephen23 2016 年 2 月 4 日
Use sub2ind:
>> A = [0,1,2;3,4,5;6,7,8;9,10,11] % your matrix
A =
0 1 2
3 4 5
6 7 8
9 10 11
>> B = [2,4,3]; % the rows you want
>> X = sub2ind(size(A),B,1:size(A,2));
>> A(X) % get the values
ans =
3 10 8

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 2 月 4 日
%Example
A= randi(10,5,6)
B =randi(5,1,6)
%---------------------
[n,m]=size(A);
idx=B+(0:n:n*(m-1))
out=A(idx)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by