How can I extract specific columns of a Matrix

1 次查看(过去 30 天)
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

回答(1 个)

Cedric
Cedric 2013-10-29
编辑:Cedric 2013-10-29
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)) ;

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by