I am learning julia in a university course and I have to do an assigment but I don’t know how to preprocess the data. Hi have a matrix with patterns distributed in rows. Its dimension is 1521×4.
The first attribute take values from 0 to 4 and I want to delete patterns (rows) whose first attribute has a value of less than 4. How could I do it using Julia?
I know that there are a lot of operations to work with matrices and arrays in julia but I don’t find a suitable one for my problem.
I have this code:
using DelimitedFiles
files_and_dirs = readdir()
for f in files_and_dirs
if filesize(f) == 0
rm(f)
end
end
filter(endswith(".lxyr"), files_and_dirs)
#Inicializamos la matriz con 1 fila y el numero de atributos de columnas
global dataset = zeros(1,4)
#134 = Numero maximo que queremos leer
for count in 1:134
name = "img"*string(count)*".lxyr"
if isfile(name) #Checkeamos si existe
matrix = readdlm(name)
global dataset = vcat(dataset, matrix)
end
end
>Solution :
To delete patterns (rows) whose first attribute has a value of less than 4, you can use a logical indexing technique in Julia. Here’s how you can modify your code to achieve this:
using DelimitedFiles
files_and_dirs = readdir()
for f in files_and_dirs
if filesize(f) == 0
rm(f)
end
end
filter(endswith(".lxyr"), files_and_dirs)
#Inicializamos la matriz con 1 fila y el numero de atributos de columnas
global dataset = zeros(1,4)
#134 = Numero maximo que queremos leer
for count in 1:134
name = "img"string(count)".lxyr"
if isfile(name) #Checkeamos si existe
matrix = readdlm(name)
# Logical indexing to filter rows with first attribute >= 4
matrix_filtered = matrix[matrix[:,1] .>= 4, :]
global dataset = vcat(dataset, matrix_filtered)
end
end
Remove the first row which was initialized with zeros
dataset = dataset[2:end,:]