Write a script that given a vector, called A, of n numbers, outputs the index of the first number divisible by 3. Sample Output: Given A = [5, 4, 6, 7, 3] divisible by 3 is:3
조회 수: 3 (최근 30일)
이전 댓글 표시
The index first number divisible by 3 is: 3
but I do not know how to get the position from the vector.
댓글 수: 0
채택된 답변
KALYAN ACHARJYA
2019년 8월 27일
편집: KALYAN ACHARJYA
2019년 8월 27일
A = [5, 4, 6, 7, 3];
idx=find(mod(A,3)==0); % Gives the index number / Position
disp(A(idx)); % Gives the those idx number in A
댓글 수: 2
KALYAN ACHARJYA
2019년 8월 28일
In A there are two numbers, which are divisible by 3, numbers are 6 (position 3) and 3 (position 5)
A = [5, 4, 6, 7, 3];
idx=find(mod(A,3)==0) % This line gives the index position
Result:
idx =
3 5
If you are interested to know the those index position number, use
disp(A(idx));
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!