How can I remove elements divisible by 3,4, and 5 in a vector?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bridgit Nata
am 21 Sep. 2016
Beantwortet: karim karim
am 10 Jan. 2020
I've created a vector x = 1:50 but I'm having trouble in how I can remove elements divisible by 3, 4, and 5, and then display it in a new vector. Thank you!
0 Kommentare
Akzeptierte Antwort
Sean de Wolski
am 21 Sep. 2016
x = 1:50;
x([3:3:50,4:4:50,5:5:50]) = []
2 Kommentare
James Tursa
am 21 Sep. 2016
@Nathalie: Note that this solution deletes the elements whose "indexes" are divisible by 3, 4, 5. In your particular example, since the indexes match up with the values one-for-one, this gives the same solution as below.
Weitere Antworten (3)
Andrei Bobrov
am 21 Sep. 2016
x = (1:50)';
out = x(all(bsxfun(@rem,x,3:5),2));
2 Kommentare
James Tursa
am 21 Sep. 2016
@Nathalie: Note that this solution deletes the elements whose "values" are divisible by 3, 4, 5. In your particular example, since the indexes match up with the values one-for-one, this gives the same solution as above.
karim karim
am 10 Jan. 2020
This is a general solution :
x=input(' x = ');
i=1;
while i<= length(x)
if mod(x(i),3)==0 || mod(x(i),4)==0 || mod(x(i),5)==0
x(i)=[];
else i=i+1;
end
end
disp(x);
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!