Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete certain rows of a matrix in julia

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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,:]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading