필터 지우기
필터 지우기

How do I modify my code to work for odd numbers?

조회 수: 1 (최근 30일)
Matt Smith
Matt Smith 2021년 2월 22일
답변: KALYAN ACHARJYA 2021년 2월 22일
So I have this code that check vector x for odd numbers and adds 5 to each of these.
x = [4 5 7 6 5 8 2 1];
x=x+mod(x,2)*5; %Finds the remainders of x elements when divided by 2. The odd numbers will always have a remainder of 1. This is multiplied by 5 and added to the odd x elements
disp(x);
How would I modify this to make it work for even numbers?
(Or is there a better way to do the original code to make it more efficient when looking for odd/even numbers?)

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 2월 22일
x=x+~mod(x,2)*5

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by