From: Charles on 13 Apr 2010 23:50 Hi, I need help summing vectors within a matrix without having to use for loops. My data is of 2 X N, i.e data=rand(2,1000). what I want is to sum every consecutive 5 columns and save all the data in 'output'. I.e output=[sum(:,1:5), sum(:,6:10).....]. thanks guys and I look forward to a quick response.
From: Walter Roberson on 14 Apr 2010 00:11 Charles wrote: > I need help summing vectors within a matrix without having to use for > loops. My data is of 2 X N, i.e > > data=rand(2,1000). > > what I want is to sum every consecutive 5 columns and save all the data > in 'output'. > I.e output=[sum(:,1:5), sum(:,6:10).....]. sum() by default works along the first dimension (that is, will add down the columns), so there will be no difference between doing all of those individual sums after breaking up into columns, compared to the much easier choice of using sum(data) to add the columns, and then break that result up into groups of 5 columns. output = reshape(sum(data), 5, []) .'; If what you wanted instead was to sum each of the sub-groups of columns along the rows, you could use output = sum(reshape(data .', 5, [], 2)) .'; This will result in a 2 x whatever array.
From: Matt Fig on 14 Apr 2010 00:12 I think this is what you want, look at the example and see if it gives you the idea. % DATA N = 15; % 2000 in your case. data = round(rand(2,N)*10) % Engine output = reshape(sum(reshape(data.',5,[])),N/5,[]).'
From: Charles on 14 Apr 2010 00:20 Thanks to all you guys for the prompt reply, I appreciate it. Problem solved.
|
Pages: 1 Prev: publish swallows results ??? Next: I want to run a function file for every 10 seconds |