Retrieving odd numbers from a matrix

23 ビュー (過去 30 日間)
Kayuri Shah
Kayuri Shah 2015 年 2 月 15 日
編集済み: Stephen23 2015 年 2 月 15 日
Within a matrix, I want to be able to retrieve only odd numbers within the matrix, and place that into its own matrix.
What I have right now is:
x = randn(1,100); odd = mod(x, (x/2))
I'm not able to get the new matrix, odd, to print with the odd values from x, even though I divided by 2, in order to return any values with a remainder. I get an error pointing to the second argument of the function mod.
Where am I going wrong with my code?
Thanks for the help!

回答 (2 件)

Andrea Nguyen
Andrea Nguyen 2015 年 2 月 15 日
Instead of doing mod(x, x/2) use mod (x, 2)
This will output the odd numbers rather than just dividing it into two. Hope that helps!

Stephen23
Stephen23 2015 年 2 月 15 日
編集済み: Stephen23 2015 年 2 月 15 日
Use MATLAB's great indexing ability:
X = randn(1,100);
Y = X(mod(X,2)==1);

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by