- Q: Are we able to flip/merge the data automatically based on the number of repetitions? A: Yes using ROT90 and setting the number of rotations as twice (multiple of 180) the repetition index minus one (so the first is not rotated).
- Q: Are we able to build a cell array of these rotated vectors? A: Yes using ARRAYFUN implicitely iterating from 1 to the number of repetitions (which should be the size of groups for summation).
- Q: Are we able to concatenate all rotated vector horizontally without developing as a CSL (necessary for a call to HORZCAT)? A: Yes using CELL2MAT.
- Q: Are we able to sum over groups of size n? A: Yes reshaping first the whole thing in a n x m array and summing over dim 1.
sum along data with different steps
7 次查看(过去 30 天)
显示 更早的评论
Dear all;
I have three data sets: This is just an example...
data1=[1 5 3 4 2 0 1 2 8 2 10 2 1]; %
data2=[1 10 ....................]
data3=[4 3 .....................]
I would like to sum by a step of 2 ,3 4 etc.. along data1 file. That means data1 becomes now:
data1_2 =[6 7 2 3 10 12] % sum of 2 numbers
data_3 =[9 6 11 14] % sum of three numbers, the last number 1 is not included
data_4 =[13 5 22] % the last number 1 is again not included
data_5 =[15 13] % the last numbers 2 and 1 are not included
I did it and it works..
But I want to improve it to do the following:
Continue the sum calculation till the same length of the original data is reached. That means:
data1_2 =[6 7 2 3 10 12 12 10 3 2 7 6]; % sum from left to right and then the right to the left till the desired length is reached
The result should be in case of summing 3 numbers for example like this:
data1_3=[9 6 11 14 13 12 3 12 9 6 1 14]
Here is my code:
##########################################
for i=1:1:length(data1); % sum of events
step=floor(length(data1)./i);
for j=1:1:step
start_idx=i*(j-1)+1;
end_idx=i+start_idx-1;
Sum_data1(i,j)=sum(data1(start_idx:end_idx));
end
end
#################################
Thanks you gentlemen for your helps…
Cheers
0 个评论
采纳的回答
Cedric
2017-10-28
编辑:Cedric
2017-10-28
Interestingly, the following seems to produce what you are looking for:
>> expandSum = @(x,n) sum(reshape(cell2mat(arrayfun(@(k)rot90(data1(1:n*floor(numel(data1)/n)), 2*(k-1)), 1:n, 'Unif', 0)), n, [])) ;
and with this function defined:
>> expandSum(data1, 2)
ans =
6 7 2 3 10 12 12 10 3 2 7 6
>> expandSum(data1, 3)
ans =
9 6 11 14 14 11 6 9 9 6 11 14
>> expandSum(data1, 4)
ans =
13 5 22 22 5 13 13 5 22 22 5 13
Of course, it may not be that useful given like this as an big ugly one-liner, so here was the thought process:
PS: I don't guarantee that it is really working in all situations, you'll have to understand and test if you want to follow this approach.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!