Skip to content

Commit ad76bb3

Browse files
committed
Added internal documentation to SliceFunction
1 parent 670f29a commit ad76bb3

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

MappedTensor.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,65 @@ function disp(mtVar)
922922

923923
%% SliceFunction - METHOD Execute a function on the entire tensor, in slices
924924
function [mtNewVar] = SliceFunction(mtVar, fhFunction, nSliceDim, vnSliceSize, varargin)
925+
% SliceFunction - METHOD Execute a function on the entire tensor, in slices
926+
%
927+
% Usage: [<mtNewVar>] = SliceFunction(mtVar,
928+
% fhFunctionHandle, nSliceDim <, vnSliceSize,> ...)
929+
%
930+
% 'mtVar' is a MappedTensor. This tensor will be sliced up along
931+
% dimensions 'nSliceDim', with each slice passed individually to
932+
% 'fhFunctionHandle', along with the slice index and any trailing
933+
% argments (...). If no return argument is supplied, the results
934+
% will be stored back in 'mtVar'. If a return argument is
935+
% supplied, a new MappedTensor will be created to contain the
936+
% results. The optional argument 'vnSliceSize' can be used to
937+
% call a function that returns a different sized output than the
938+
% size of a single slice of 'mtVar'. In that case, a new tensorsl
939+
% 'mtNewVar' will be generated, and it will have the size
940+
% 'vnSliceSize', with the dimension 'nSliceDim' having the same
941+
% length as in the original tensor 'mtVar'.
942+
%
943+
% "Slice assign" operations can be performed by passing in a
944+
% function that takes no input arguments for 'fhFunctionHandle'.
945+
%
946+
% Note that due to Matlab not making available the number of
947+
% return arguments that an anonymous function delivers, all
948+
% functions passed to SliceFunction must return AT LEAST ONE
949+
% argument.
950+
%
951+
% For example:
952+
%
953+
% mtVar(:) = abs(fft2(mtVar(:, :, :)));
954+
%
955+
% is equivalent to
956+
%
957+
% SliceFunction(mtVar, @(x)(abs(fft2(x)), 3);
958+
%
959+
% Each slice of the third dimension of mtVar, taken in turn, is
960+
% passed to fft2 and the result stored back into the same slice of
961+
% mtVar.
962+
%
963+
% mtVar2 = SliceFunction(mtVar, @(x)(fft2(x)), 3);
964+
%
965+
% This will return the result in a new MappedTensor, with
966+
% temporary storage.
967+
%
968+
% mtVar2 = SliceFunction(mtVar, @(x)(sum(x)), 3, [1 10 1]);
969+
%
970+
% This will create a new MappedTensor with size [1 10 N], where
971+
% 'N' is the length along dimension 3 of 'mtVar'.
972+
%
973+
% SliceFunction(mtVar, @()(randn(10, 10)), 3);
974+
%
975+
% This will assign random numbers to each slice of 'mtVar'
976+
% independently.
977+
%
978+
% SliceFunction(mtVar, @(x, n)(x .* vfFactor(n)), 3);
979+
%
980+
% The second argument to the function is passed the index of the
981+
% current slice. This line will multiply each slice in mtVar by a
982+
% scalar corresponding to that slice index.
983+
925984
% - Get tensor size
926985
vnTensorSize = size(mtVar);
927986

0 commit comments

Comments
 (0)