To use vcat(a,b) and hcat(a,b), one must match the number of columns or number of rows in the matrices a and b.
When constructing a matrix using vact(a, b) or hcat(a, b) in a loop, one needs an initial matrix a (like a starting statement). Although all the sub-matrices are created in the same manner, I might need to construct this initial matrix a outside of the loop.
For example, if the loop condition is for i in 1:w, then I would need to pre-create a using i = 1, then start the loop with for i in 2:w.
If there is a nested loop, then my method is very awkward. I have thought the following methods, but it seems they don't really work:
Use a dummy
a, deleteaafter the loop. From this question, we cannot delete row in a matrix. If we use another variable to refer to the useful rows and columns, we might waste some memory allocation.Use
reshape()to make an empty dummya. It works for 1 dimension, but not multiple dimensions.julia> a = reshape([], 2, 0) 2×0 Array{Any,2} julia> b = hcat(a, [3, 3]) 2×1 Array{Any,2}: 3 3 julia> a = reshape([], 2, 2) ERROR: DimensionMismatch("new dimensions (2,2) must be consistent with array size 0") in reshape(::Array{Any,1}, ::Tuple{Int64,Int64}) at ./array.jl:113 in reshape(::Array{Any,1}, ::Int64, ::Int64, ::Vararg{Int64,N}) at ./reshapedarray.jl:39
So my question is how to work around with vcat() and hcat() in a loop?
Edit: Here is the problem I got stuck in:
There are many gray pixel images. Each one is represented as a 20 by 20 Float64 array. One function foo(n) randomly picks n of those matrices, and combine them to a big square.
If n has integer square root, then foo(n) returns a sqrt(n) * 20 by sqrt(n) * 20 matrix.
If n does not have integer square root, then foo(n) returns a ceil(sqrt(n)) * 20 by ceil(sqrt(n)) * 20 matrix. On the last row of the big square image (a row of 20 by 20 matrices), foo(n) fills ceil(sqrt(n)) ^ 2 - n extra black images (each one is represented as zeros(20,20)).
My current algorithm for foo(n) is to use a nested loop. In the inner loop, hcat() builds a layer (consisting ceil(sqrt(n)) images). In the outer loop, vcat() combines those layers.
Then dealing with hcat() and vcat() in a loop becomes complicated.
vcatorhcatentirely by building aVector{Vector{T}}dynamically by usingpush!orappend!, and then convert the result to a matrix when you're done (the final conversion is unlikely to have a performance impact on your routine except in unusual cases). I suspect this will be significantly faster than looping over calls tovcatas long as you're careful to take advantage of column-major order... - Colin T Bowerspush!orappend!to each element of that matrix. Are there better ways to deal with this situation? - Jay Wong