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

2 views (last 30 days)
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

Accepted Answer

dpb
dpb on 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...

More Answers (1)

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

Categories

Find more on Creating and Concatenating Matrices 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!