Powerspawn
Powerspawn

Reputation: 135

Sub-tensor of a tensor with arbitrary degree in matlab

I have a function where I input a tensor T of arbitrary degree d which is of size n1 x n2 x ... x nd. I want to output the tensor T(1:r,1:r,...,1:r). In other words, an r x r x ... x r sub-tensor with d number of r's. I'm having difficulty working around the variable number 1:r's. Ideally I'd like to do this without reshaping T if possible.

Upvotes: 3

Views: 70

Answers (1)

flawr
flawr

Reputation: 11628

This is quite straightforward to do with cell arrays, consider following example:

% example setup
a = ones(3,3,3,3);
r = 2;
% create indices in cell array
b = cell(1,ndims(a));
b(:) = {1:r};
% evaluate
c = a(b{:});
disp(size(c))

Upvotes: 4

Related Questions