I need to remove any column in a matrix which has same values in it. I designed it using a for-loop in MATLAB. I wanted to know if a better/faster way exists using 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!
Here’s a simple one-liner using the functions DIFF and ANY: