필터 지우기
필터 지우기

How do I get matlab to list all numbers from 1 to 100 divisible by 6?

조회 수: 153 (최근 30일)
Christina Kersten
Christina Kersten 2018년 9월 15일
댓글: Walter Roberson 2020년 10월 10일
I only have basic matlab knowledge, so the simpler the code the better! Something that would be easy for me to understand. Thanks!

답변 (1개)

Stephen23
Stephen23 2018년 9월 15일
편집: Stephen23 2018년 9월 15일
Method zero: colon and times:
>> 6.*(1:16)
ans =
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
Method one: mod and logical indexing:
>> v = 1:100; % any vector
>> v(mod(v,6)==0)
ans =
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
Method three: linear indexing:
>> v(6:6:end) % v starts from 1
ans =
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
Method three: colon:
>> 6:6:100
ans =
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
Method four: cumsum:
>> cumsum(6*ones(1,16))
ans =
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
Method five: rdivide, fix, eq, and logical indexing:
>> w = v./6;
>> v(fix(w)==w)
ans =
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
  댓글 수: 4
Jake Jacobs
Jake Jacobs 2020년 10월 10일
im terrible at this, how do you get 16 at the start though?
Walter Roberson
Walter Roberson 2020년 10월 10일
16 is floor(100/6) -- which is to say the maximum number of full multiples of 6 that fits into 100.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by