How to remove zeros from an array????

3 vues (au cours des 30 derniers jours)
suchismita
suchismita le 22 Fév 2016
Modifié(e) : Stephen23 le 22 Fév 2016
I have a array as
a=1:20
Columns 1 through 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Columns 16 through 20
16 17 18 19 20
and another array as
y =
1 2 3 13 15 16 17 19 20
then i want to find the elements of y which are not in a so
b=~ismember(a,y);
b1=bsxfun(@times,a,b)
b1 =
Columns 1 through 15
0 0 0 4 5 6 7 8 9 10 11 12 0 14 0
Columns 16 through 20
0 0 18 0 0
now i want to remove the 0's finally i want a matrix x
x=
4 5 6 7 8 9 10 11 12 14 18
please help me...
thank you

Réponse acceptée

Stephen23
Stephen23 le 22 Fév 2016
Modifié(e) : Stephen23 le 22 Fév 2016
Simply use logical indexing directly on a (those b variables are not required):
>> a(~ismember(a,y))
ans =
4 5 6 7 8 9 10 11 12 14 18
or the simplest solution is to use setdiff:
>> setdiff(a,y)
ans =
4 5 6 7 8 9 10 11 12 14 18

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by