hstack
result = hstack(a, b, ...)
Input: a, b, ... · Output: result
What it is
Named equivalents of bracket-literal block concatenation ([a, b] horizontally / [a; b] vertically). Each of the 2+ required arguments is a scalar, Vec, or Mat (real or complex — any complex argument makes the whole result complex); hstack needs matching row counts, vstack needs matching column counts.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = hstack(a, b, ...) | a —Named equivalents of bracket-literal block concatenation ([a, b] horizontally / [a.b —b] vertically).... repeatablenot documented yet | Mat CMator CMat if any input was complex | Returns a Mat (or CMat if any input was complex). |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
print((1,2,3,4) as matrix(2,2)) # cannot reshape a list
print(hstack(("a",), ("b",))) # expected a number, found list
from agent-guide
n_per_class = 60
c0x = normal(0, 0.6, n_per_class, seed=1)
c0y = normal(0, 0.6, n_per_class, seed=2)
c1x = normal(4, 0.6, n_per_class, seed=3)
c1y = normal(4, 0.6, n_per_class, seed=4)
Xc0 = hstack(reshape(c0x, n_per_class, 1), reshape(c0y, n_per_class, 1))
Xc1 = hstack(reshape(c1x, n_per_class, 1), reshape(c1y, n_per_class, 1))
Xc = vstack(Xc0, Xc1) # (120, 2)
n = 2 * n_per_class
idx = 0 to n - 1
y = floor(idx / n_per_class) # 0 for the first 60 rows, 1 for the last 60
function one_hot(labels, rows, k)
m = zeros(rows, k)
for i = 0 to rows - 1
m[i, labels[i]] = 1
end for
return m
end function
Y = one_hot(y, n, 2)
net = mlp_classifier(2, [8], 2, seed=42)
trained = net.fit(Xc, Y, epochs=200, lr=0.1, loss="cross_entropy")
function accuracy(model, X, Ytrue, rows)
logits = model.predict(X)
correct = 0
for i = 0 to rows - 1
if argmax(logits[i, :]) == argmax(Ytrue[i, :]) then
correct = correct + 1
end if
end for
return correct / rows
end function
acc = accuracy(trained, Xc, Y, n)
print("training accuracy = {acc:.4f}")
training accuracy = 1.0000
from quickstart
How it works
hstack(a, b, ...) / vstack(a, b, ...) — explicit, named equivalents of the bracket-literal block-concatenation magic ([a, b] / [a; b], see eval_matrix_literal_blocks), for scripts that would rather spell the operation out than rely on [...]'s implicit dispatch — same "also give it a plain function name" precedent as panel/subplot. Both need 2+ arrays; a single argument would make the call redundant.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.
See also
vstack — documented in the same entry.