### Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#" onclick="window._kiq.push(['set', { 'event': 'Turn off MT' }]);">ここ</a>」をクリックしてください。

## sum of matrix omitting one dimension

Rafael Schwarzenegger

### Rafael Schwarzenegger (view profile)

さんによって質問されました 2017 年 11 月 2 日

### Stephen Cobeldick (view profile)

さんによって コメントされました 2017 年 11 月 2 日
Stephen Cobeldick

### Stephen Cobeldick (view profile)

さんの 回答が採用されました
Hi, I would like to ask how I could sum over a n dimensional matrix except one dimension, so the output is a vector. The ndims are not known in forehand. The summation is giving always a vector. (In my case a marginal pdf in statistics). Something like sum(K(:) except i-th dimension)
The best in a cyclus ( ndims not known).
For example having matrix K, the sums would be [6 22], [12 16], [10 18]
K(:,:,1) =
0 1
2 3
K(:,:,2) =
4 5
6 7

KL

### KL (view profile)

2017 年 11 月 2 日
You have to rephrase your question, what is your input and what do you expect as output?
Cedric Wannaz

### Cedric Wannaz (view profile)

2017 年 11 月 2 日
There may be a mistake in your example. Isn't the first 12 supposed to be 22?
Rafael Schwarzenegger

### Rafael Schwarzenegger (view profile)

2017 年 11 月 2 日
Yeah 22, thank you.
I have as an input a m^n matrix and as and output n vectors of the length m.

サインイン to comment.

## 2 件の回答

2017 年 11 月 2 日

### Stephen Cobeldick (view profile)

2017 年 11 月 2 日
採用された回答

Here is one very simple way:
>> d = 3; % dimension
>> v = 1:ndims(K);
>> v([1,d]) = v([d,1]);
>> s = sum(reshape(permute(K,v),size(K,d),[]),2)
s =
6
22
It works by swapping the first and the desired dimension, then reshaping so that all trailing dimensions are reduced to one. The sum is then trivially along each row.
Note that the method I show here also has the significant advantage that it does not create any variables with copies of the data from the input array: this will be important for scaling to larger input arrays. Also note that slow and complex arrayfun or cellfun are not required for this task!

#### 4 件のコメント

Stephen Cobeldick

### Stephen Cobeldick (view profile)

2017 年 11 月 2 日
Just change the dimension d. It is on the very first line of code, I even labeled it for you.
" It gives [6 22]. "
No, it gives all of the answers that you showed in your question. How do I know this? Because I tested it. You just need to change the dimension value d. It would be trivial to do this in a loop, if you need to.
Rafael Schwarzenegger

### Rafael Schwarzenegger (view profile)

2017 年 11 月 2 日
Oh yeah, that is correct. (I thought first, it is the total dimension.) Thank you very much!
Stephen Cobeldick

### Stephen Cobeldick (view profile)

2017 年 11 月 2 日
@Rafael Schwarzenegger: the dimensions of the input array are automatically measured using ndims(K).

サインイン to comment.

### Cedric Wannaz (view profile)

2017 年 11 月 2 日

Here is one way, but there is probably a simpler approach:
buffer = arrayfun(@(k) permute(KL, circshift(1:ndims(KL), k-1)), 1:ndims(KL), 'UniformOutput', false) ;
s = cellfun(@(M) sum(reshape(M, [], size(M,2))), buffer, 'UniformOutput', false) ;
where s is a cell array of sum vectors.
EDIT : I won't have time, but maybe you can see how dimensions work with SHIFTDIM and understand how to have the outputs ordered as needed:
buffer = arrayfun(@(k) shiftdim(KL, k-1), 1:ndims(KL), 'UniformOutput', false) ;
s = cellfun(@(M) sum(reshape(M, [], size(M,2))), buffer, 'UniformOutput', false) ;
PS : you'll have to test whether it really does what you need. The approach is based on the fact that MATLAB reads memory column first

#### 4 件のコメント

Stephen Cobeldick

### Stephen Cobeldick (view profile)

2017 年 11 月 2 日
@Rafael Schwarzenegger: See my answer for a simpler, neater, and much more efficient solution.
Cedric Wannaz

### Cedric Wannaz (view profile)

2017 年 11 月 2 日
As mentioned by Stephen, arrayfun and cellfun are not necessary. They are hidden loops and I used them for conciseness. You can build a FOR loop if you prefer, and again, as Stephen mentions it would have the advantage not to require storing copies of your data (if you are dealing with large arrays).
Rafael Schwarzenegger

### Rafael Schwarzenegger (view profile)

2017 年 11 月 2 日
@Cedric Wannaz: Thank you anyway.

サインイン to comment.

Translated by