user5916581
user5916581

Reputation: 87

Keeping part of a matrix Matlab

I have a matrix

enter image description here

I have two other matrices (B and C) which is the row index of A from top to bottom.

enter image description here

I want the new A to have only row groups of B and C.

New A:

enter image description here

How to do this?

Upvotes: 1

Views: 50

Answers (1)

ibezito
ibezito

Reputation: 5822

You'll need to do the following:

  1. concatenate B and C into a single vector by using [B;C];
  2. remove duplicated indices from B and C by using the function unique (in your example, there are no duplications between the two vectors).
  3. change A to accordingly

You can use the following syntax:

A = A(unique([B;C]),:);

If you know for a fact that B and C don't contain duplications, you can omit the unique function call, and just write:

A = A([B;C],:);

Upvotes: 3

Related Questions