0
$\begingroup$

I have a $D$-way tensor of dimensions $n\times n \times \dots \times n$ $(D)$- times. I want to sum the First vectors in all directions. For example, let $\boldsymbol{H}$ is 3-way tensor of dimensions $n \times n \times n$. Then the desired output, $Res = \boldsymbol{H}(:,1,1)+\boldsymbol{H}(1,:,1)'+reshape(\boldsymbol{H}(1,1,:),n,1)$. How can I perform this automatically for higher dimensional tensors? I tried using rot90 function in MATLAB, but it rotates only the first slice of the Tensor.

H(:,:,1) = [1 2 3; 4 5 6; 7 8 9];
H(:,:,2) = 10*[1 2 3; 4 5 6; 7 8 9];
H(:,:,3) = 100*[1 2 3; 4 5 6; 7 8 9];

Res = H(:,1,1)+H(1,:,1)'+reshape(H(1,1,:),3,1);

Here $Res = \begin{bmatrix}1\\4\\7 \end{bmatrix} + \begin{bmatrix}1\\2\\3 \end{bmatrix} + \begin{bmatrix}1\\10\\100 \end{bmatrix}$

for higher dimensions: $D=4, Res = H(:,1,1,1)+H(1,:,1,1)'+reshape(H(1,1,:,1),3,1)+reshape(H(1,1,1,:),3,1);$

and so on for $D=5,6,..$. I do not want to do this summation manually.

$\endgroup$
2
  • $\begingroup$ Here's how I'd do it in Julia (which has fast for-loops) $$\eqalign{ &{\tt res = zeros(n)} \\ &{\tt for\;k=1:n} \\ &\qquad {\tt res[k] = H[k,1,1,1] + H[1,k,1,1] + H[1,1,k,1] + H[1,1,1,k]} \\ &{\tt end} }$$ $\endgroup$
    – greg
    May 22, 2023 at 10:44
  • $\begingroup$ Hi Greg. Thank you. The line inside the for loop is only for 4-way tensor. Is there a way to do it for any dimensional tensor? $\endgroup$
    – Neuling
    May 22, 2023 at 10:50

2 Answers 2

2
$\begingroup$

With the permute function I guess you might be able to do something like this, for the example that you have given with H is a $3\times 3\times 3$ tensor, which has the vector dimension and tensor dimension as $3$,

nTensorDim = 3;
nVectorDim = 3;

sz = size(H);
inds = repmat({1},1,ndims(H));
inds{1} = 1:sz(1);

sum = zeros(nVectorDim, 1);
for i=1:nTensorDim
    idx = 1:nTensorDim;
    idx(i) = 1;
    idx(1) = i;
    y = permute(H, idx);
    sum = sum + y(inds{:});
end

The variable sum would contain the result. Hope this is the tensor operation that you have explained!

$\endgroup$
4
  • $\begingroup$ Hi Thank you, but what is populateX() function and what is the variable 'n' inside the for loop? $\endgroup$
    – Neuling
    May 22, 2023 at 9:38
  • $\begingroup$ I edited my question with an example. I am trying to sum the first vectors in all directions of a higher order array. Thus, the resulting summed variable is a vector of size same as the size of the tensor, i.e. size n of a nxnxn tensor. $\endgroup$
    – Neuling
    May 22, 2023 at 10:18
  • $\begingroup$ Thanks for the clarification, I guess the code snippet should work for the example you have given, I have edited the answer again. $\endgroup$ May 22, 2023 at 11:11
  • $\begingroup$ Can this be extended for higher order tensors, where I can sum the rest of the elements of the tensor H? Can you please refer to this for clarification? math.stackexchange.com/q/4718760/706629 $\endgroup$
    – Neuling
    Jun 20, 2023 at 8:39
2
$\begingroup$

Julia has a convenient "..." (aka splat) operator, which deconstructs a vector into a list, so you can do this

s = zeros(n)        # sum accumulates in this vector
ndx = ones(Int,D)   # set every index to 1 (or 2, or 3)
for k = 1:D
    x = ndx[k]
    for j = 1:n
        ndx[k] = j  # set kth index to j
        s[j] = s[j] + H[ndx...]  # SPLAT
    end
    ndx[k] = x    # reset kth index
end

The above algorithm can be trivially modified to calculate the sum the second vector from each dimension by initializing ndx to a vector of $2$s, or the third vectors with a vector of $3$s, etc.

I'm not a Matlab guy, but it must have something equivalent to the splat operator.

Update

Here's a modification which uses a symbolic colon (at the end of the ndx array) to vectorize the inner loop

s, ndx = zeros(n), [ones(Int,D)..., :]
for k = 1:D
    x, ndx[k] = ndx[k], ndx[end]
    s, ndx[k] = s+H[ndx[1:D]...], x
end

While it has a nicer appearance, it's actually 400% slower than the previous version.

$\endgroup$
1
  • $\begingroup$ Can this be extended for higher order tensors, where I can sum the rest of the elements of the tensor H? Can you please refer to this for clarification? math.stackexchange.com/q/4718760/706629 $\endgroup$
    – Neuling
    Jun 20, 2023 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.