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

4 Ansichten (letzte 30 Tage)
Shane
Shane am 2 Jul. 2014
Bearbeitet: Star Strider am 3 Jul. 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

Akzeptierte Antwort

dpb
dpb am 3 Jul. 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...

Weitere Antworten (1)

Shane
Shane am 3 Jul. 2014
That's makes sense.

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