Clear Filters
Clear Filters

avoid zero value from vector

4 views (last 30 days)
Israt Jahan
Israt Jahan on 22 Nov 2016
Answered: Stephen23 on 22 Nov 2016
Hi I have got two vectors from running my matlab code such as
x=[5 0 9 0 4 0 7 0]
y=[0 9 0 7 0 2 0 6]
now i want to use these two vector as input in other code. so i need to avoid these zero values from above vector and my input will be like
x=[5 9 4 7]
y=[9 7 2 6]
would anyone help me to how can write the matlab code to get above vectors.

Accepted Answer

Stephen23
Stephen23 on 22 Nov 2016
Simply use logical indexing:
>> x =[5 0 9 0 4 0 7 0];
>> xnew = x(x~=0)
xnew =
5 9 4 7

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!