필터 지우기
필터 지우기

Count using mod function

조회 수: 12 (최근 30일)
Devika Sunil
Devika Sunil 2020년 9월 26일
편집: Ameer Hamza 2020년 9월 26일
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일
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);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by