Removing rows and columns of a matrix based on elements of another matrix

I want to remove rows and columns of a matrix based on the elements of another matrix. Both matrix are not of the same size.
Example
A = [1 2 3
4 5 6];
B = [1 2 3 4 5 6
7 8 9 10 11 12
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29
30 31 32 33 34 35];
I want to delete say a row and column in B that corresponding to A(1,3) and A(2,3), which is the best way to go about this

Answers (2)

l1=A(1,3),l2=A(2,3);
ind=[l1;l2];
B(ind,:)=[]
B(:,ind)=[]
%A in your example is 2x3 , then A(3,3) does'nt exist
I think you are looking for the [] operator:
B(A(1,3),:) = [];
B(:,A(2,3))= [];
You should not do it one after the other as the indexing might change if the indices are not ordered from smallest to largest. It is better to do them all at once:
B(A(1,:),:)=[];
B(:,A(2,:))=[];

Categories

Asked:

on 4 Sep 2012

Community Treasure Hunt

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

Start Hunting!