How can I remove elements divisible by 3,4, and 5 in a vector?

조회 수: 6 (최근 30일)
Bridgit Nata
Bridgit Nata 2016년 9월 21일
답변: karim karim 2020년 1월 10일
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!

채택된 답변

Sean de Wolski
Sean de Wolski 2016년 9월 21일
x = 1:50;
x([3:3:50,4:4:50,5:5:50]) = []
  댓글 수: 2
James Tursa
James Tursa 2016년 9월 21일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Andrei Bobrov
Andrei Bobrov 2016년 9월 21일
x = (1:50)';
out = x(all(bsxfun(@rem,x,3:5),2));
  댓글 수: 2
James Tursa
James Tursa 2016년 9월 21일
@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.

댓글을 달려면 로그인하십시오.


George
George 2016년 9월 21일
You can do this using mod().

karim karim
karim karim 2020년 1월 10일
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);

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by