Count using mod function

x is a random array of 100 elements. x1 is another 100-element array containing values from 1 to 100 in steps of 1. I need to set a counter that counts the number of odd elements in x and x1 using mod function. Also I need to check how many elements in x and x1 are multiples of 5.
How do i do this?

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 9 月 26 日
編集済み: Ameer Hamza 2020 年 9 月 26 日

0 投票

The following example show how to find the even number in a vector using mod(). You can extend it to other cases
x = [1 4 3 2 1 2 9 7 3 2];
y = mod(x, 2) == 0; % because even number divided by 2 give remainder of 0.
n = nnz(y);

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2020 年 9 月 26 日

編集済み:

2020 年 9 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by