One can easily concatenate (stack up) four arrays by using:
B = cat(3, A, A, A, A); % here A is 2-dimensional M x N matrix and B is 3-dimensional M x N x 4 stack
How is it possible to concatenate (stack) n
arrays to get M x N x ... x n
array? The dimenisonality of A
array is known (or easy to figure as numel(size(A))
)
B = A; % A is M x N 2-dimensional array here
for ii = 1 : n-1
B = cat(3, B, A);
end
is very poor,
B = nan([size(A),n]);
for ii = 1 : n
B(:, :, ii) = A;
end
is quite slow too.
The motivation is to find the array in S
-stack that is closest to the array A
.
If I want to find element of vector S
closest to a scalar a
it is super simple:
distances = abs(S - a);
FOO = find(distances == min(distances),1);
but this opperation S - a
works only for arrays of same size or when a
is a scalar.
In more general manner I want to perform a dot-opperation on B
and A
, like B.*A
, B./A
, etc.