Delete any column with the same values ​​in MATLAB

I need to delete any column in a matrix with the same values ​​in it. I developed it using for-loop in MATLAB. I wanted to know if there is a better / faster way to use vectorization.

mat = [ 0.56 0.2 1 0 45; 0.566 0.2 4 0 45; 0.52 0.2 6 0 45; 0.56 0.2 6 0 41 ]; [row col] = size(mat) ; bitmat = true(1,col) ; for i = 2:row, tf = (mat(i-1,:) == mat(i,:)) ; bitmat = bitmat & tf ; end mat(:,bitmat) = [] ; 

Thanks!

+4
source share
1 answer

Here is a simple single line layer using the DIFF and ANY functions:

 mat = mat(:,any(diff(mat,1))); 
+4
source

All Articles