Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select odd numbers from a matrix?

Tags:

julia

How do you select odd numbers from a matrix larger than 29?

like image 233
Biljana Radovanovic Avatar asked Nov 01 '25 14:11

Biljana Radovanovic


1 Answers

You can use filter:

filter(x->isodd(x)&&x>29, M)

Here, x->isodd(x)&&x>29 is an anonymous function, specifying your filter criterium, and M is your matrix.

Example:

julia> M = rand(1:50, 3,3)
3×3 Array{Int64,2}:
 20  42  35
 23   6  31
 28   4   4

julia> filter(x->isodd(x)&&x>29, M)
2-element Array{Int64,1}:
 35
 31

Alternatively, you can use array comprehensions:

julia> [x for x in M if isodd(x) && x>29]
2-element Array{Int64,1}:                
 35                                      
 31                                      
like image 193
carstenbauer Avatar answered Nov 04 '25 07:11

carstenbauer