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

4 ビュー (過去 30 日間)
The index first number divisible by 3 is: 3
but I do not know how to get the position from the vector.

採用された回答

KALYAN ACHARJYA
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 件のコメント
Shuoze Xu
Shuoze Xu 2019 年 8 月 28 日
The first number can be divided by 3 is 6,
6 is the third number in this vector, so the position of first number is 3.
i want to know how to get ouput of position is 3 in this question
KALYAN ACHARJYA
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 ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by