Selected multiple values from a matrix

78 Ansichten (letzte 30 Tage)
Jakob Sørensen
Jakob Sørensen am 21 Mär. 2014
Kommentiert: Jakob Sørensen am 21 Mär. 2014
Lets say i got a 10x10 matrix, lets call it M , and I want to select the data from [a,b] and [c,d]. Of course i could do this one at a time like this:
val_1 = M(a,b);
val_2 = M(c,d);
But I get my points handed in two separate vector, lets call them q and p, one with row indexes and one with column indexes like such:
q = [a c];
p = [b d];
Additionally I would like the values I select, to be combined in a vector as well. That is
vals = [val_1 val_2];
So my question is whether there is a clever way to do this? I mean it would be quite easy to just loop though the coordinates one-by-one, but there has to be a smarter, clearer way to do this? I hope you understand what I mean, it got a bit confusing :-p

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 21 Mär. 2014
Bearbeitet: Azzi Abdelmalek am 21 Mär. 2014
q = [a c];
p = [b d];
idx=sub2ind(size(M),q,p);
out=M(idx)
  1 Kommentar
Jakob Sørensen
Jakob Sørensen am 21 Mär. 2014
I knew there had to be a clever way, thank you very much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by