Predefined array too large

I predefined a vector E to be the same size as vector D.

E = typeof(D)(undef, length(D))

In most cases, I expect E to have the same number of elements as D, but I still want to be able to handle the case where E has fewer elements than D. In those cases, this declaration produces a vector with extra undefined elements that I would like to trim off.

For example, it might show

E = Array{Rational{Int128},1}[[1//1, 0//1, 0//1], [0//1, 1//1, 0//1], [0//1, 0//1, 1//1], #undef, #undef, #undef]

I tried to use a while loop to pop! of the end elements

while length(E) > numInE     
     pop!(E)
end

but this returned an UndefRefError: access to undefined reference.

Is there a way I can trim the end of an array efficiently?

You can use resize!. Also, instead of the typeof thing, look into the similar function.

3 Likes

Thanks! Will do :slight_smile: