Find elements having a specific row and column numbers in a matrix
Ältere Kommentare anzeigen
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 Kommentare
"Of course, I can solve this in a for-loop which is time conssumming"
I doubt that. Why do you think that?
"I wonder if this is the fastest approach?"
Probably a loop. Lets compare against using SUB2IND (which itself is a perfectly reasonable approach):
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 = c; % preallocate
for k = 1:numel(v)
v(k) = A(r(k),c(k));
end
toc
tic
w = A(sub2ind(size(A),r,c));
toc
isequal(v,w)
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
Davide Masiello
am 21 Dez. 2022
I suspect sub2ind might just apply the little string of code I devised in my answer, or something very similar to it.
Akzeptierte Antwort
Weitere Antworten (1)
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)
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!