retrieve value from specific location of matrix

2 ビュー (過去 30 日間)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 1 日
Hi suppose I have a matrix as follows:
m =
1 1
1 3
1 5
2 2
2 4
3 1
3 3
3 5
each row of m is the row,column index respectively.
And I have another matrix I.
I need to find the values of I of location m. How can I do that?
Is there anybody to help me? Thanks

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 6 月 1 日
use sub2ind to create from coordinates an index that points to the position.
m = [1 1
1 3
1 5
2 2
2 4
3 1
3 3
3 5]
I = rand(5);
sub2ind(size(I),m(:,1),m(:,2))
To skip the overhead you can apply the engine of sub2ind directly (2d adaptation):
(m(:,2)-1)*size(I,1) + m(:,1)
  3 件のコメント
Matt Fig
Matt Fig 2011 年 6 月 2 日
You are using SUB2IND incorrectly. SUB2IND returns the indices for use into an array, not the indexed array. So, use it like this:
IDX = sub2ind(size(I),m(:,1),m(:,2)); % Create index vector
I(IDX) % Index into I with IDX.
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 2 日
yes this is correct.
Thanks.

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

その他の回答 (0 件)

カテゴリ

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