Clear Filters
Clear Filters

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

4 views (last 30 days)
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 Comments
Torsten
Torsten on 21 Dec 2022
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 on 21 Dec 2022
I suspect sub2ind might just apply the little string of code I devised in my answer, or something very similar to it.

Sign in to comment.

Accepted Answer

Torsten
Torsten on 21 Dec 2022
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

More Answers (1)

Davide Masiello
Davide Masiello on 21 Dec 2022
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

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!