MATLAB: Extract columns from matrix based on a vectors values

columndimensionmatrixvalue

I want to extract the columns of a matrix A (mxn), based on a vector B's values into a new matrix C, meaning:
Which also should be dependent on B's dimensions (1xm):
For example:
A=[1,1,1,0;
2,1,0,1];
B=[3,4];
Then C should be:
C=[1,0;
0,1].
I want this to work for any dimensions of n and m.
I have tried:
C=A(:,B(1,1):1:B(1,size(B,2)))
but it doesn't work for B=[3,1] since it takes column by column.
Thanks!

Best Answer

I think this should do it for you:
C = A(:,B)