Reputation: 35
x = [1 2 3 4 5
1 2 3 0 0];
I want to get off all zeros and merge the two rows
newx = x(:,1) + x(:,2)(nonzeros)
= [1 2 3 4 5 1 2 3];
Upvotes: 1
Views: 58
Reputation: 16791
nonzeros
will give you nonzeros in a column vector, you just have to orient the original matrix properly and transpose into a row vector (if that's what you want):
>> newx = nonzeros(x.').'
newx =
1 2 3 4 5 1 2 3
Upvotes: 3
Reputation: 19689
newx=x.'; %Taking Transpose
% Converting the given matrix into a column vector and then taking transpose again
% (since you require answer as a row vector)
newx=newx(:).' ;
newx(newx==0)=[] %Removing zeros
or using reshape
:
newx = reshape(x.',1,[])
newx(newx==0)=[] %Removing zeros
Result:
newx =
1 2 3 4 5 1 2 3
Upvotes: 1