Effacer les filtres
Effacer les filtres

How to "filter" a matrix using a vector quantity?

4 vues (au cours des 30 derniers jours)
Shane
Shane le 2 Juil 2014
Modifié(e) : Star Strider le 3 Juil 2014
I have a matrix [8x3] and a vector quantity that's [6x1].
What I'm trying to do is pick out the three values from the vector within the matrix. Here is the code I'm running.
x=[1;2;3;4;5;6]
x =
1
2
3
4
5
6
y=[1,2,3;2,2,3;3,3,4;4,5,7;5,6,7;6,7,8;7,4,3;8,2,3]
y =
1 2 3
2 2 3
3 3 4
4 5 7
5 6 7
6 7 8
7 4 3
8 2 3
N=y(x)
N =
1
2
3
4
5
6
Obviously my code has more numbers, but essentially the above is what I'm trying to do. However, I want to keep the entire matrix from rows 1 through 6 intact in the answer. Is there any command that can do that? In other words, I want this as the final answer:
N =
1 2 3
2 2 3
3 3 4
4 5 7
5 6 7
6 7 8
I just typed the last part in in, but how do I do that for bigger number sets with a certain command automatically? Thanks

Réponse acceptée

dpb
dpb le 3 Juil 2014
Almost there...
N=y(x,:);
the x vector is the row index, : picks up all the columns.
doc colon
for the details. Also might working thru the exercises/examples in the "Getting Started" section on basic array manipulation and addressing...

Plus de réponses (1)

Shane
Shane le 3 Juil 2014
That's makes sense.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by