How to remove columns from matrix?
Show older comments
I have a matrix A and I want to remove from this matrix each column that contain the value -1.
A= [-1.192 -1.020 -1 -1.050 -1 -1 -1.070;
-1.213 -1.096 -1 -1.045 -1 -1 -1.102;
-1.036 -1.061 -1 -1.085 -1 -1 -1.137;
-1.176 -1.120 -1 -1.004 -1 -1 -1.115;
-1.124 -1.034 -1 -1.073 -1 -1 -1.134;
-1.202 -1.145 -1 -1.078 -1 -1 -1.057;
-1.127 -1.023 -1 -1.056 -1 -1 -1.066]
the size of A is 7*7 and I want to have a matrix 7*4 after removing colums? How can I do please?
Accepted Answer
More Answers (3)
% A(:, any(A == -1)) = [] %% Please try this.
% revised 'any' to 'all'. 'any' means the columns in which there is any '-1'; 'all'means the columns in which they are all '-1'.
A(:, all(A == -1)) = []
2 Comments
James Tursa
on 15 Apr 2015
"all" instead of "any" to achieve the goal "... remove columns that entirely contain the value -1 ..."
for that particular matrix you want
B = A(:, [1 2 4 7]);
If this is a more general problem, that's a bit more tricky. Should the column consist entirely of -1 or just one single -1 is sufficient for eliminating it?
Sahar abdalah
on 15 Apr 2015
0 votes
3 Comments
Star Strider
on 15 Apr 2015
Have you tried my code to see if it works on your larger matrix?
Sahar abdalah
on 15 Apr 2015
Star Strider
on 15 Apr 2015
What do you mean by ‘remove the index’? The resulting matrix will be smaller, so you will have to address its columns differently than you did in the original matrix. You will have to rewrite your code to do that.
Categories
Find more on Creating and Concatenating Matrices 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!