英文:
Delete certain rows of a matrix in julia
问题
我正在大学课程中学习Julia,我必须完成一个作业,但我不知道如何预处理数据。我有一个矩阵,其中模式分布在行中。其维度为1521x4。
第一个属性的取值范围是0到4,我想删除第一个属性值小于4的模式(行)。如何在Julia中做到这一点?
我知道Julia中有许多用于处理矩阵和数组的操作,但我找不到适合我的问题的操作。
我有以下代码:
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)
# 初始化一个包含1行和4列属性的矩阵
global dataset = zeros(1, 4)
# 134 = 我们想要读取的最大数
for count in 1:134
name = "img" * string(count) * ".lxyr"
if isfile(name) # 检查文件是否存在
matrix = readdlm(name)
global dataset = vcat(dataset, matrix)
end
end
英文:
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 1521x4.
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
答案1
得分: 2
以下是您要翻译的代码部分:
我认为您的代码执行以下操作(将您问题中的所有代码写成一行):
```python
dataset = [readdlm(name) for name in string.("img",1:134,".lxyr") if isfile(name) && filesize(name) > 0]
如果您要过滤具有至少4个值的第一行的矩阵数据集:
filter!(m -> all(m[1,:] .>= 4), matrices)
如果您有一个如下的矩阵:
m = collect(reshape(1:16,4,4))
然后,您可以使用以下代码选择具有超过某个阈值的值的行:
@view m[[all(row .>= 2) for row in eachrow(m)],:]
<details>
<summary>英文:</summary>
I think your code does this (all code from your question written in one line):
dataset = [readdlm(name) for name in string.("img",1:134,".lxyr") if isfile(name) && filesize(name) > 0]
It is not clear what you need - filter matrices of rows from a matrix
If you want to filter dataset for matrices that have in first row values of at least 4:
filter!(m -> all(m[1,:] .>= 4), matrices)
If you have a matrix such as:
m = collect(reshape(1:16,4,4))
Than you can select rows having values above some threshold with a code such as:
@view m[[all(row .>= 2) for row in eachrow(m)],:]
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论