Find elements having a specific row and column numbers in a matrix

조회 수: 2 (최근 30일)
Hello,
I have a simple question but could not find a single command which can do the job. I explain via an example. Consider the following matrix
A = [ 85 45 15 28 11 4 59
79 81 87 32 94 10 85
90 79 64 62 93 25 62
29 41 82 66 100 16 72
37 33 72 59 22 41 98];
Assume that r = [2 5 2 2 5 2 1], c = [3 7 7 1 2 1 6]; What I want, are 7 elements of A whose first component comes from vector r but whose second component comes from vector c. I mean, A(2,3), A(5,7), A(2,7), A(2,1),A(5,2), A(2,1), A(1,6). Of course, I can solve this in a for-loop which is time conssumming. In Matlab, if I write A(r,c) then I get something else which I do not want. The answer to my question is actually the diagonal elements of this matric, that is to say,
diag(A(r,c))
but, I wonder if this is the fastest approach?
Do you know a better way?
Thanks in advance!
Babak
  댓글 수: 3
Torsten
Torsten 2022년 12월 21일
Interestingly, the solution from @Davide Masiello seems to be the fastest:
A = [85,45,15,28,11,4,59;79,81,87,32,94,10,85;90,79,64,62,93,25,62;29,41,82,66,100,16,72;37,33,72,59,22,41,98];
r = [2,5,2,2,5,2,1];
c = [3,7,7,1,2,1,6];
tic
v = A(size(A,1)*(c-1)+r);
toc
Elapsed time is 0.001910 seconds.
Davide Masiello
Davide Masiello 2022년 12월 21일
I suspect sub2ind might just apply the little string of code I devised in my answer, or something very similar to it.

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2022년 12월 21일
A = [ 85 45 15 28 11 4 59
79 81 87 32 94 10 85
90 79 64 62 93 25 62
29 41 82 66 100 16 72
37 33 72 59 22 41 98];
r = [2 5 2 2 5 2 1];
c = [3 7 7 1 2 1 6];
A(sub2ind(size(A),r,c))
ans = 1×7
87 98 85 79 33 79 4

추가 답변 (1개)

Davide Masiello
Davide Masiello 2022년 12월 21일
If the answer is the diagonal, then I'd use the diag function.
However, if you are still interested in generalizing the code, you could do
A = [ 85 45 15 28 11 4 59
79 81 87 32 94 10 85
90 79 64 62 93 25 62
29 41 82 66 100 16 72
37 33 72 59 22 41 98];
r = [2 5 2 2 5 2 1];
c = [3 7 7 1 2 1 6];
A(size(A,1)*(c-1)+r)
ans = 1×7
87 98 85 79 33 79 4

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by