Add Cells/Matrices Row by Row

2 次查看(过去 30 天)
Avik Mahata
Avik Mahata 2021-8-13
编辑: dpb 2021-8-13
I have a large number of matrices/vectors. And I need to add then row by row. So I have 100 different 50x10 matrices. Now I want to add Row 1 (first cell) + Row 1 (second cell)+...., and repeat it for all 60 rows and get a final 50x10 matrix. I have them all in cell format.
  2 个评论
Image Analyst
Image Analyst 2021-8-13
编辑:Image Analyst 2021-8-13
And your question is......?
My question is why do you have 100 different matrices? Hopefully they're not all separate variables but you're just getting a new matrix in a loop that's iterating 100 times. Or are all 100 matrices in individual cells of a 1-D cell array?
My second question is why are you insisting on cell arrays when, if the matrices are all the same size 50x10, when a normal 3-D double array would be much faster and more efficient?
Next question is : can you attach your data in a .mat file, or attach your script that generates the data?
Next question is did you just try a simple for loop - should be very very simple.
By the way, 100 is not large as far as array sizes or memory usage.
Avik Mahata
Avik Mahata 2021-8-13
Thanks for taking time to reply. Below is the details. Any suggestions will be extremely helpful for my reserach.
Well here is the problem, I have a large matrix of about 600,000 rows and 14 coulumns. This data is a 6000 different bins with 100 columns each. So what I am trying to do is add (Row1 + Row101+ Row201....+..), (Row2 + Row 102+ Row 202 + ....) and get a final matrix of 100x14.
I am trying to elaborate more below,
a=rand(12,2)
a =
0.5862 0.1559 % Row 1
0.6509 0.7744 % Row 2
0.8756 0.7171 % Rowl 3
0.3699 0.7807 % Row 4
0.5258 0.6832 % Row 5
0.0857 0.9956 % Row 6
0.0644 0.5571 % Row 7
0.1754 0.6236 % Row 8
0.6654 0.6843 % Row 9
0.4061 0.1972 % Row 10
0.7061 0.8972 % Row 11
0.3061 0.9720 % Row 12
Now what I wanted to do is to bin 4 rows together. So we have 3 bins like below,
Bin 1
0.5862 0.1559 % Row 1
0.6509 0.7744 % Row 2
0.8756 0.7171 % Rowl 3
0.3699 0.7807 % Row 4
Bin 2
0.5258 0.6832 % Row 5
0.0857 0.9956 % Row 6
0.0644 0.5571 % Row 7
0.1754 0.6236 % Row 8
Bin 3
0.6654 0.6843 % Row 9
0.4061 0.1972 % Row 10
0.7061 0.8972 % Row 11
0.3061 0.9720 % Row 12
Now what I wanted to do is take a average of the (Row1+ Row 5+ Row 9), (Row 2+ Row 6 + Row 10) and so on. Once I add them up I will end up with a matrix of , 4x 2.
What I trying is create a loop llike below hoping that the loop repeats 100 times and create 100 rows. But its not doing it somehow. I am doing something wrong in the loop.
clc
clear all
M=dlmread('segmentaf');
M(1:101:end,:) = [];
L = length(M);
for x = 1:1:100
for a = 1:100:(L-101)
for b = a+100
Q = M(a:b,13);
end
Q3 = Q1+Q2;
end
end

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2021-8-13
"matrix of about 600,000 rows and 14 columns. This data is a 6000 different bins with 100 columns each. So what I am trying to do is add (Row1 + Row101+ Row201....+..), (Row2 + Row 102+ Row 202 + ....) and get a final matrix of 100x14. "
Using your smaller example, but same logic works, the "dead ahead" solution is
>> x=rand(12,2) % some sample data
x =
0.8147 0.9572
0.9058 0.4854
0.1270 0.8003
0.9134 0.1419
0.6324 0.4218
0.0975 0.9157
0.2785 0.7922
0.5469 0.9595
0.9575 0.6557
0.9649 0.0357
0.1576 0.8491
0.9706 0.9340
>>
% the engine
N=4; % number of elements in group
M=size(x,1)/N; % number of bins -- must be evenly divisible
S=zeros(M,size(x,2); % preallocate
for i=1:M
S(i,:)=sum(x(i:N:end,:));
end
Show what we got...
>> S
S =
2.4046 2.0347
1.9682 1.4368
0.5631 2.4416
>>
>> x(1,1)+x(5,1)+x(9,1) % illustrate first term gets right answer...
ans =
2.4046
>>
  2 个评论
Avik Mahata
Avik Mahata 2021-8-13
Thanks for the response. But actually I am adding those 3 bins of 4x2 matrices to add row by row. So it should give a final 4x2 matrix.
dpb
dpb 2021-8-13
编辑:dpb 2021-8-13
So, switch N from 4 to 3 -- but that's not what your example asked...
"Now what I wanted to do is to bin 4 rows together. So we have 3 bins like below, "
The logic is generic regardless of the size as long as size(data,1)/N is integer.
>> N=3; % changed mind, groups of 3, not four
>> M=size(x,1)/N; % compute # bins given N
>> S=zeros(M,2); % preallocate again
>> for i=1:M,S(i,:)=sum(x(i:N:end,:));end % and compute
>> S % what we get
S =
2.9715 1.9270
2.2426 2.7158
2.1526 3.3057
2.1568 0.9698
>> x(1,1)+x(4,1)+x(7,1)+x(10,1) % again, just check got it right...
ans =
2.9715
>>

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by