Retain all initial even value elements, add all odd values with 1

1 回表示 (過去 30 日間)
Lucio Shi
Lucio Shi 2021 年 11 月 23 日
コメント済み: Lucio Shi 2021 年 11 月 23 日
Using random number generator, generate ten integer values in the form of 1-D array. Store it in a variable named A2Q10. How to retain all initial even value elements, add all odd values with 1? Now I know A2Q10 = randi([1 10],1,10) is to generate. But how to form a new 1-D array to only add 1 to odd value elements from the generated 1-D array. Please help me!

回答 (1 件)

KSSV
KSSV 2021 年 11 月 23 日
Hint: Read about mod. If mod(x,2) is 1 then x is odd.
mod(4,2) % even number
ans = 0
mod(5,2) % odd number
ans = 1
  1 件のコメント
Lucio Shi
Lucio Shi 2021 年 11 月 23 日
I don't know how to make it a new 1-D array.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by