フィルターのクリア

Matrix Indexig or how to access values of a matrix

2 ビュー (過去 30 日間)
Philipp ter Schiphorst
Philipp ter Schiphorst 2020 年 5 月 5 日
Hallo dear forum,
I just don't find a problem for my actually simple problem.
I have the following matrix:
A = magic(4)
I now want the values ​​of the matrix in position:
value1 = A(1,1)
value2 = A(2,2)
value3 = A(3,1)
My actual matrix indexes are in a vector in this form:
index = [1 1;2 2;3 1];
My solution (that is obviously wrong) would be:
solution = A(index)
Is there an easy way without using loops?
Thank you so much!

採用された回答

Tommy
Tommy 2020 年 5 月 5 日
You could convert to linear indices:
solution = A(sub2ind(size(A),index(:,1),index(:,2)));
  1 件のコメント
Philipp ter Schiphorst
Philipp ter Schiphorst 2020 年 5 月 6 日
Thank you very much, that was exactly what I was looking for

サインインしてコメントする。

その他の回答 (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