Skip to content

Instantly share code, notes, and snippets.

@pabloferz
Created January 3, 2021 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabloferz/9db7832579e55caf76389b94ade69195 to your computer and use it in GitHub Desktop.
Save pabloferz/9db7832579e55caf76389b94ade69195 to your computer and use it in GitHub Desktop.
Avoid vcat
# Esta opción es suficiente para pasar el ejercicio,
# pero abajo hay una opción aún más rápida
function remove_in_each_row_no_vcat(img, column_numbers)
@assert size(img, 1) == length(column_numbers) # same as the number of rows
m, n = size(img)
local img′ = similar(img, m, n-1) # create a similar image with one less column
for (i, j) in enumerate(column_numbers)
# EDIT THE FOLLOWING LINE and split it into two lines
# to avoid using `vcat`.
img′[i, 1:j-1] .= img[i, 1:j-1]
img′[i, j:end] .= img[i, j+1:end]
end
img′
end
# Esta opción es todavía más rápida, utiliza el macro @view
# @view evita hacer una copia del vector.
function remove_in_each_row_no_vcat(img, column_numbers)
@assert size(img, 1) == length(column_numbers) # same as the number of rows
m, n = size(img)
local img′ = similar(img, m, n-1) # create a similar image with one less column
for (i, j) in enumerate(column_numbers)
# EDIT THE FOLLOWING LINE and split it into two lines
# to avoid using `vcat`.
img′[i, 1:j-1] .= @view img[i, 1:j-1]
img′[i, j:end] .= @view img[i, j+1:end]
end
img′
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment