Created
January 3, 2021 22:46
-
-
Save pabloferz/9db7832579e55caf76389b94ade69195 to your computer and use it in GitHub Desktop.
Avoid vcat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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