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

2 次查看(过去 30 天)
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

采纳的回答

dpb
dpb 2014-7-3
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...

更多回答(1 个)

Shane
Shane 2014-7-3
That's makes sense.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by