% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/S3_matvec.R
\name{sweep.HDF5Matrix}
\alias{sweep.HDF5Matrix}
\title{Broadcast a vector over an HDF5Matrix (sweep)}
\usage{
\method{sweep}{HDF5Matrix}(
  x,
  MARGIN = 2L,
  STATS,
  FUN = "*",
  check.margin = TRUE,
  paral = NULL,
  threads = NULL,
  compression = NULL,
  ...
)
}
\arguments{
\item{x}{An \code{HDF5Matrix} (the matrix to sweep over).}

\item{MARGIN}{Integer. \code{2} (default, sweep over columns) or
\code{1} (sweep over rows). Corresponds to \code{byrows}:
\code{MARGIN = 1} means \code{byrows = TRUE}.}

\item{STATS}{An \code{HDF5Matrix} with one row or one column
(the vector to broadcast). Must be in the same HDF5 file as \code{x}.}

\item{FUN}{Character. Operation: \code{"+"}, \code{"-"},
\code{"*"} (default), \code{"/"}, \code{"pow"}.}

\item{check.margin}{Ignored (kept for S3 signature compatibility).}

\item{paral}{Logical or NULL.}

\item{threads}{Integer or NULL.}

\item{compression}{Integer (0-9) or NULL.}

\item{...}{Ignored.}
}
\value{
A new \code{HDF5Matrix}.
}
\description{
S3 method of \code{base::sweep()} for \code{HDF5Matrix} objects.
Broadcasts a 1-row \code{HDF5Matrix} (acting as the STATS vector)
across every row or column of the matrix, element-wise.
}
\examples{
\donttest{
fn <- tempfile(fileext = ".h5")

mat <- matrix(rnorm(100), 10, 10)
X   <- hdf5_create_matrix(fn, "data/X", data = mat)

# STATS must be an HDF5Matrix with one row or one column
# Create a 1-row vector with column means
col_means_vec <- colMeans(as.matrix(X))
stats_hdf5    <- hdf5_create_matrix(fn, "data/col_means",
                                     data = matrix(col_means_vec, 1, 10))

# Column-center X (MARGIN = 2)
X_c <- sweep(X, 2, stats_hdf5, "-")

# Verify first column is centered
all.equal(as.matrix(X_c)[, 1],
          mat[, 1] - col_means_vec[1])

hdf5_close_all()
unlink(fn)
}

}
