Cell array summation when arrays are different sizes

조회 수: 8 (최근 30일)
Mau
Mau 2020년 8월 26일
편집: Stephen23 2020년 8월 26일
I am trying to some cell arrays containing matrices of different sizes stored at designated index points to arrays. See the below desired outputs.
% First Cell Input
cellname = {...
[1 1 1; ...
2 2 2; ...
3 3 3], ...
[1 1 1; ...
2 2 2; ...
3 3 3; ...
4 4 4; ...
5 5 5; ...
6 6 6], ...
[1 1 1; ...
2 2 2; ...
3 3 3; ...
4 4 4; ...
5 5 5; ...
6 6 6; ...
7 7 7; ...
8 8 8; ...
9 9 9]};
% Summed rowise to the below matrix
output1 =[9
18
27
16
30
36
21
24
27]
output1b =[3 3 3; ...
6 6 6; ...
9 9 9; ...
8 8 8; ...
10 10 10; ...
12 12 12; ...
7 7 7; ...
8 8 8; ...
9 9 9]
% Second Cell Input
cellname2 = {...
[1 2 3; ...
1 2 3; ...
1 2 3], ...
[1 2 3 4 5 6; ...
1 2 3 4 5 6; ...
1 2 3 4 5 6], ...
[1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9; ...
1 2 3 4 5 6 7 8 9]};
% Summed columnwise to the below matrix
output2 = [9 18 27 24 30 36 21 24 27]
output2b = [3 6 9 8 10 12 7 8 9; ...
3 6 9 8 10 12 7 8 9; ...
3 6 9 8 10 12 7 8 9]

채택된 답변

Stephen23
Stephen23 2020년 8월 26일
편집: Stephen23 2020년 8월 26일
Without any padding with extra zeros/NaN/etc.
Generate some indices to use accumarray to sum only the required elements:
>> in1 = {[1,1,1;2,2,2;3,3,3],[1,1,1;2,2,2;3,3,3;4,4,4;5,5,5;6,6,6],[1,1,1;2,2,2;3,3,3;4,4,4;5,5,5;6,6,6;7,7,7;8,8,8;9,9,9]}
in1 =
[3x3 double] [6x3 double] [9x3 double]
>> fun = @(m)ndgrid(1:size(m,1),1:size(m,2));
>> [i1r,i1c] = cellfun(fun,in1,'uni',0);
>> i1r = vertcat(i1r{:});
>> i1c = vertcat(i1c{:});
>> tmp = vertcat(in1{:});
>> z1a = accumarray(i1r(:),tmp(:))
z1a =
9
18
27
24
30
36
21
24
27
>> z1b = accumarray([i1r(:),i1c(:)],tmp(:))
z1b =
3 3 3
6 6 6
9 9 9
8 8 8
10 10 10
12 12 12
7 7 7
8 8 8
9 9 9
And similarly for the other direction:
>> in2 = {[1,2,3;1,2,3;1,2,3],[1,2,3,4,5,6;1,2,3,4,5,6;1,2,3,4,5,6],[1,2,3,4,5,6,7,8,9;1,2,3,4,5,6,7,8,9;1,2,3,4,5,6,7,8,9]}
>> [i2r,i2c] = cellfun(fun,in2,'uni',0);
>> i2r = horzcat(i2r{:});
>> i2c = horzcat(i2c{:});
>> tmp = horzcat(in2{:});
>> z2a = accumarray(i2c(:),tmp(:)).'
z2a =
9 18 27 24 30 36 21 24 27
>> z2b = accumarray([i2r(:),i2c(:)],tmp(:))
z2b =
3 6 9 8 10 12 7 8 9
3 6 9 8 10 12 7 8 9
3 6 9 8 10 12 7 8 9
Probably could be generalized for any arbitrary input arrays and requested dimension (that exercise is left up to the reader).
  댓글 수: 4
Stephen23
Stephen23 2020년 8월 26일
편집: Stephen23 2020년 8월 26일
"...can you please explain, whats happening here"
This approach is based on accumarrray. A detailed explanation of accumarray is beyond the scope of one comment on this thread, so I suggest you carefully read its documentation, try its examples, and read some related threads on this forum.
accumarray neatly allows specific array elements to be summed together, we just have to define appropriate indices. That is what i1r and i1c are: indices that tell accumarray where the values should go in its output array. Before being included in the output array, the default function (sum) is applied to those values.
Take a look at the tmp array (which is just your data concatenated together (using different values would have been clearer than these repeated values)):
tmp =
1 1 1
2 2 2
3 3 3
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
and the corresponding indices:
i1r =
1 1 1
2 2 2
3 3 3
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
  • An index value of 1 tells accumarray to allocate the corresponding tmp value to the 1st output element
  • An index value of 2 tells accumarray to allocate the corresponding tmp value to the 2nd output element
  • An index value of 3 tells accumarray to allocate the corresponding tmp value to the 3rd output element
  • etc.
So based on those indices we have told accumarray to send the values 1, 1, 1, 1, 1, 1, 1, 1, and 1 (your fake data should have been chosen with a bit more variety to make things clearer) to the output array's first element, and to apply the default function (sum) to them before allocating the function output (9) to the first element of the output array. Ditto for all of the other elements of the index/data arrays.
For z1b and z2b we also include column indices, but otherwise everything works the same.
Mau
Mau 2020년 8월 26일
편집: Mau 2020년 8월 26일
Thanks for the detailed explanation. I will also read more about accumarray.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Bruno Luong
Bruno Luong 2020년 8월 26일
편집: Bruno Luong 2020년 8월 26일
I do just one, let you adapt to the second.
cellname = {...
[1 1 1; ...
2 2 2; ...
3 3 3], ...
[1 1 1; ...
2 2 2; ...
3 3 3; ...
4 4 4; ...
5 5 5; ...
6 6 6], ...
[1 1 1; ...
2 2 2; ...
3 3 3; ...
4 4 4; ...
5 5 5; ...
6 6 6; ...
7 7 7; ...
8 8 8; ...
9 9 9]};
s1 = cellfun('size',cellname,1)
n = max(s1);
Ap = cellfun(@(a) [a;zeros(n-size(a,1),size(a,2))], cellname, 'unif', 0);
output1 = sum(cell2mat(Ap),2)
output1b = sum(cat(3,Ap{:}),3) % NOTE output1 is sum(output1b,2)
  댓글 수: 1
Mau
Mau 2020년 8월 26일
Thanks a lot. This also works fine and it's easier to grasp.

댓글을 달려면 로그인하십시오.


KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 8월 26일
"I am trying to some cell arrays containing matrices of different sizes stored at designated index points to arrays. See the below desired outputs"
Option 1: If you are looking for sum of all array elments within the cell array
data=cell2mat(result(cell_array));
result=sum(data(:))
Option 2: If you are looking for sum of invividial elments (single matrix) within the cell array
result=zeros(1,length(cell_array))
for i=1:length(cell_array)
temp=cell_array{i};
result(i)=sum(temp(:));
end
result
You may avoid the loop also here
  댓글 수: 1
Mau
Mau 2020년 8월 26일
The loop gives the following result.
result =
18 63 135

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by