I am trying to rebin 30 values into 1 using for loops but am running into trouble. I am trying to do the sum of 30 values --> put into cell 1, then sum the next 30 values --> put into cell 2 and so on. In other words, I am trying to do this:
secdist(1,1) = sum(framedist(1:30));
secdist(2,1) = sum(framedist(31:61));
secdist(3,1) = sum(framedist(62:92))
Here is my code:
nbins=ceil(s/30); %number of bins. Grouping of 30 values (=1second) into 1 bin
secdist= zeros(nbins,1);
for i2=1:nbins, i=1:31:(s-30);
secdist(i2,1) = sum(framedist(i:(i+30))); %%sum of distance over 30 poses (1second) -- not working
end
However, this is not working like I would like. Does anyone have any idea how I could do this?

 Accepted Answer

Try this:
framedist = randi(9, 1, 90);
A = reshape(framedist(:), 30, []);
secdist = sum(A)
It reshapes ‘framedist’ to a (30xN) matrix, then sums the columns.

4 Comments

Thanks for your reply. Unfortunately, this function doesn't work for what I am trying to do. I can't seem to use the reshape function using s/30 rows for framedist because "product of known dimensins, 2620 (ceil(s/30)), not divisible into total number of elements, 78589 (size of framedist, s)."
This runs without error:
framedist = randi(9, 1, 78589); % Create ‘framedist’
framedist1 = framedist(1 : fix(numel(framedist)/30)*30); % First 78570 Elements
framedist2 = framedist(fix(numel(framedist)/30)*30+1 : end); % Last 19 Elements
A = reshape(framedist1(:), 30, []);
secdist = [sum(A) sum(framedist2)];
This is one solution. I leave it to you to determine if this does what you want. (I created a version of ‘framedist’ to test my code. You can obviously delete that line.)
Thank you this is perfect, and neat little workaround for the frames that don't fit in the available bins!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!