Cell array summation when arrays are different sizes
2 次查看(过去 30 天)
显示 更早的评论
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]
0 个评论
采纳的回答
Stephen23
2020-8-26
编辑:Stephen23
2020-8-26
Without any padding with extra zeros/NaN/etc.
>> 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
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.
更多回答(2 个)
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)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!