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?

 Réponse acceptée

Star Strider
Star Strider le 15 Avr 2015
A more general solution:
A = reshape(A(~arrayfun(@isequal, A, -ones(size(A)))),size(A,1),[]);
It first finds a logical array of vectors that are equal to -1 using arrayfun, takes the values of the matrix that are not those values and creates a new vector from them. It then takes that vector and reshapes it to the row size of the original matrix to create the new ‘A’ matrix.

5 commentaires

pfb
pfb le 15 Avr 2015
wow this is very cool.
But does it work if one of the columns has just some -1's and other numbers?
Star Strider
Star Strider le 15 Avr 2015
It wouldn’t work in the instance you describe, nor would the more efficient approach by ‘ChangYuan’ work, and for the same reason.
The reason is that then there would be unequal row or column (or both) sizes, and that would throw an error for a numeric matrix. You would have to cast it as a cell array if you needed for random elements to be empty. Cell arrays tolerate that, numeric arrays do not.
Eliminating entire single or multiple columns or rows is permitted in numeric arrays. Removing random elements — unless they resulted in all rows or columns still having equal numbers of elements — is not.
pfb
pfb le 15 Avr 2015
Modifié(e) : pfb le 15 Avr 2015
yes this is clear to me.
I was more thinking to the instance where just one -1 would be a reason for eliminating the whole column. But then again, that's not the problem at hand.
I did not notice ChangYuan's solution. I never knew of the commands "any" and "all", nor I am very well acquainted with arrayfun. I like this!
BTW, is the latter really necessary? What's wrong with
~(A== -ones(size(A))
?
Star Strider
Star Strider le 15 Avr 2015
Did you try it yourself to see what it does? (My code does a version of what you intend with that.)
BTW, I always test my code to be sure it works before I post it (or clearly label it ‘untested code’ if I can’t). It saves time and enhances credibility.
I did test it. Here is what I get
>> A = [-1 2 -1 3 -1; 2 -1 -1 0 1];
>> A==-ones(size(A))
ans =
1 0 1 0 1
0 1 1 0 0
>> arrayfun(@isequal, A, -ones(size(A)))
ans =
1 0 1 0 1
0 1 1 0 0
It seems to me that they are equivalent, at least for this minimal example. I jotted down the matrix randomly, making sure that at least one column was composed of -1 alone. Maybe I was lucky. I have to admit I did not embark in systematic testing. Anyway, that is why I asked.

Connectez-vous pour commenter.

Plus de réponses (3)

N/A
N/A le 15 Avr 2015
Modifié(e) : N/A le 16 Avr 2015
% 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 commentaires

James Tursa
James Tursa le 15 Avr 2015
"all" instead of "any" to achieve the goal "... remove columns that entirely contain the value -1 ..."
N/A
N/A le 16 Avr 2015
Modifié(e) : N/A le 16 Avr 2015
Yeah. Just a while after I shut down my PC last night, I realized it should be 'all'.
A(:, all(A == -1)) = []
Thanks!

Connectez-vous pour commenter.

pfb
pfb le 15 Avr 2015
Modifié(e) : pfb le 15 Avr 2015
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
Sahar abdalah le 15 Avr 2015

0 votes

My goal is to remove the columns that entirely contain the value -1. the problem is more general, I have a matrix A: size m * n with m number of test samples and n is the label of the class . The size of A in my case is 1000 * 1181. I want to remove the column and also their index.

3 commentaires

Star Strider
Star Strider le 15 Avr 2015
Have you tried my code to see if it works on your larger matrix?
Sahar abdalah
Sahar abdalah le 15 Avr 2015
yes it work but I want to remove the index also
Star Strider
Star Strider le 15 Avr 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.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by