How can I extract specific columns of a Matrix

11 views (last 30 days)
ghazale
ghazale on 29 Oct 2013
Edited: Cedric on 29 Oct 2013
For example I have this matrix
A = [1 0 0 0;0 0 0 0;0 1 0 0;0 0 0 0]
I want just extract the columns that they have one, (in this matrix will be column 1 and 2), and then show it in another matrix. I want to do this function for a big matrix. so the question must be d = [1 0;0 0;0 1;0 0]
Thanks

Answers (1)

Cedric
Cedric on 29 Oct 2013
Edited: Cedric on 29 Oct 2013
Here is an example
>> A = [1 0 0 0;0 0 0 0;0 1 0 0;0 0 0 0]
A =
1 0 0 0
0 0 0 0
0 1 0 0
0 0 0 0
>> colId = any(A, 1)
colId =
1 1 0 0
>> d = A(:,colId)
d =
1 0
0 0
0 1
0 0
Putting all that together, you would perform the following operation:
>> d = A(:,any(A,1)) ;

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!