bsanth
bsanth

Reputation: 57

Extract rows from matrix and make a new matrix in MATLAB

I have a matrix

X = [1 1;2 2;3 3;4 4];
Y = [2 4];

I want a resulting matrix z to have just rows 2 and 4 (the values in Y) of X. That is,

Z = [2 2;4 4];

Any solutions?

Upvotes: 0

Views: 2659

Answers (1)

bnaul
bnaul

Reputation: 17656

Z = X(Y,:);

This is a pretty easily researched question in my opinion: the first result for "MATLAB matrix indexing" answers your question and has a lot more general information about selecting parts of MATLAB matrices.

Upvotes: 8

Related Questions