MATLAB: Removing components of a matrix without turning it into a vector

indexingmatrix manipulation

Hello I'm trying to remove the Inf values from my matrix Z without turning it into a vector. I have an idea of how to do it using loops, and I know Z(1,:)=[] + Z(:,4)=[] will work as I want it to but I wanted to see if there's a quicker way because it seems like this should be a trivial thing for MATLAB. Z is actually a sample matrix, the matrix I am working with is larger and within a function, so the Inf locations will always be different.
Thanks!
Z =
Inf Inf Inf Inf
4 6 13 Inf
10 12 19 Inf
11 18 25 Inf
>> Z(Z==Inf)=[]
Z =
4 10 11 6 12 18 13 19 25

Best Answer

ii = isinf(Z);
out = Z(~all(ii,2),~all(ii));
or
ii = ~isinf(Z);
out = reshape(Z(ii),max(sum(ii)),[]);