Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

applyDistMat3D

Apply function for class DistMat3D


Description

Apply function to values in a 3-D distance matrix. The 3-D distance matrix is an S4-class in hsdar to efficiently store distance values in hyperspectral datasets.

Usage

## S4 method for signature 'DistMat3D'
apply(X, MARGIN, FUN, ..., simplify = TRUE)

Arguments

X

Object of class 'DistMat3D'.

MARGIN

A vector giving the subscripts (dimensions) of the DistMat3D-object which the function will be applied over (see examples).

FUN

Function to be applied. Matched with match.fun.

...

Further arguments passed to FUN.

simplify

Currently ignored.

Value

Depending on the length of the return value of the specified function, objects of classes numeric or matrix are returned.

Author(s)

Lukas Lehnert

See Also

Examples

data(spectral_data)

## Part I: Create an object of class DistMat3D
## Calculate all possible NRI - combinations for WorldView-2-8
spec_WV <- spectralResampling(spectral_data, "WorldView2-8",
                              response_function = FALSE)
nri_WV <- nri(spec_WV, recursive = TRUE)

## Get all NRI-values as numeric vector
nri_values <- as.numeric(t(as.matrix(getNRI(nri_WV, 
                                            getFiniteNri(nri_WV)))))

## Create object of class DistMat3D
dmat <- distMat3D(nri_values, 8, 45)


## Part II: Apply function mean to values in the new object
## Calculate mean value of all samples for all indices 
meanIndexVals <- apply(dmat, MARGIN = 1, FUN = mean)
## Convert to DistMat3D
meanIndexVals <- distMat3D(meanIndexVals, 8, 1)

## Same but for array
nri_WV_dat <- as.array(dmat)
meanIndexVals_arr <- apply(nri_WV_dat, MARGIN = c(1, 2), FUN = mean)
## Convert to DistMat3D
meanIndexVals_arr <- distMat3D(meanIndexVals_arr)

## Test if equal
all(meanIndexVals_arr == meanIndexVals)


## Calculate mean value of all indices wihtin each sample
meanSampleVals <- apply(dmat, MARGIN = 3, FUN = mean)
meanSampleVals_arr <- apply(nri_WV_dat, MARGIN = 3, FUN = mean, 
                            na.rm = TRUE)
## Test if equal
all(meanSampleVals == meanSampleVals_arr)

## User-defined function (in this case the median)
quant <- function(x)
  return(quantile(x,  probs = 0.5))
## Apply user defined function to all samples for all indices 
medianIndexVals <- apply(dmat, MARGIN = 1, FUN = quant)

hsdar

Manage, Analyse and Simulate Hyperspectral Data

v1.0.3
GPL
Authors
Lukas W. Lehnert [cre, aut] (<https://orcid.org/0000-0002-5229-2282>), Hanna Meyer [ctb], Joerg Bendix [ctb]
Initial release
2020-08-04

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.