フィルターのクリア

pick one value from each row in a matrix, to get vector

1 回表示 (過去 30 日間)
Kim
Kim 2014 年 6 月 8 日
回答済み: André Paradis 2014 年 6 月 8 日
I have a 500x8 matrix, lets call this X, and I have a 500x1 vector, lets call it Y, the latter contains numbers between 1 and 8. These numbers indicate the number in each row that I want from the matrix X. So if Y(2,1)=3, then I need X(2,3). So by picking a number in each row of matrix X, I need to get a vector, lets call it Z. I dont know how to get this vector Z, in a quick way (not using a loop). Thank you for your help.

採用された回答

John D'Errico
John D'Errico 2014 年 6 月 8 日
編集済み: John D'Errico 2014 年 6 月 8 日
X(sub2ind(size(X),1:500,Y))
Easily enough done without sub2ind also, but might as well use the existing tool. If you would insist though...
X(1:500 + 500*(Y-1))

その他の回答 (1 件)

André Paradis
André Paradis 2014 年 6 月 8 日
for i=1:500
Z=X(i,Y(i))
end
Where Y is a column vector

カテゴリ

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