Avoiding cross product for accessing the matrix

Let say I have matrix A = magic(10). I want to get few selected values of A. Let say A(5,10), A(6,10), A(5,5). If for row I have vector r = [5 6 5] and c = [10 10 5], Then when I do
A(r,c), instead of three values I get 3x3. it is due to fact that for each value in r is paired with each value in c. How can I avoid this. I want that for the first value of r take first value of c and so on.
Actually I want to calculate the time for function, therefore, I have to take minimum instruction to compute.
Sorry for any inconvenience caused.

 採用された回答

Oleg Komarov
Oleg Komarov 2011 年 11 月 26 日

1 投票

pos = sub2ind(size(A), r,c)
A(pos)

3 件のコメント

Junaid
Junaid 2011 年 11 月 26 日
Thank you Oleg, It is really two line of code which really helped me.
Could you please do me an other favor. Let say A is the matrix of size 100 x 100, A is kind of lookup table.
and There is an other matrix B that has the size let say 10 x 100. Where each col is histogram. Now for Query col vector Q size of 10 x 1.
I want to get resultant matrix R of size(B), where each cell value in R, is obtained by looking the values of Q and B in A.
let say:
R(1,1) = A( Q(1), B(1,1));
R(5,5) = A(Q(5)), B(5,5));
I hope you understand the scenario.
Thanks a lot in advance.
Daniel Shub
Daniel Shub 2011 年 11 月 26 日
You should ask this as another question.
Andrei Bobrov
Andrei Bobrov 2011 年 11 月 26 日
Hi Oleg! Finally then, you're back!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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