Sum every 10 elements of a 40x1 matrix in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter kickapoo
  • Start date Start date
  • Tags Tags
    Code Matlab
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
11 replies · 4K views
kickapoo
Messages
5
Reaction score
0
hello every one.

I want to create a routine in matlab

Let A be a (40,1) matrix.
I want to create a routine that every 10 steps of A will take the sum and place it in a new matrix.

ty
 
Physics news on Phys.org
shoehorn said:
What have you tried?

I am trying something but as a noob in MATLAB i am stuck

%a=ones(20,1)
%a=[1 1 1 1 1 2 2 2 2 2]'
idx=[1:size(a)]'
%a1 klimaka
k=5;
a0=0

for idx=[a0+k]
a1=1:k:10
sum_a1=sum(a(1:idx,1))
a1=k-a0
end
for idx=[a1+k]
sum_a2=sum(a(a1+1:idx,1))
a2=a1+k
end



with is code above i have manage to take the sum the first 2 times . I want the routine to take the sum in stable step ( example every 10 ) and put them in a new matrix.

Am I near at all ?
 
matonski said:
Like this?

sum(reshape(A,10,4))'


ty for time.. reshape don't work :(

i came up with this...

function [steps]= steps(data,k)
%function [sum_new]= stepsum(data,k)
%input data(:,1)
%output sum of data every k steps
k;
a=data;
idx=[1:length(a)]';
ii=length(a)/k;
sum_new=zeros(ii,1);
mean_new=zeros(ii,1);
max_new=zeros(ii,1);
min_new=zeros(ii,1);

for n=1:length(a)/k;
idx(n)=[n*k];
sum_new(n)=sum(a((n-1)*k+1:n*k));
mean_new(n)=mean(a((n-1)*k+1:n*k));
max_new(n)=max(a((n-1)*k+1:n*k));
min_new(n)=min(a((n-1)*k+1:n*k));
end

sum_new;
mean_new;
max_new;
min_new;
steps=[sum_new mean_new max_new min_new]
 
What do you mean reshape doesn't work? For example, if I let A = (1:40)', my above line of code gives me [55 155 255 355]', which is the sum of the first 10 entries, followed by the sum of the next 10 entries, etc. Is that now what you wanted?
 
Instead of putting the sums in an entirely new and distinct variable each time, you might find it better simply to append each of the sums into a given matrix. This should have the benefit that any other code you write that depends on these sums can access them through some pretty straightforward matrix subscripting.

Moreover, since this is a well-defined operation that you might need to perform repeatedly, it really belongs in a function. Many approaches are possible, but a simple first approach would be to use cell arrays and the cellfun() command to perform the heavy lifting. Save the following code as blockSum.m in your Matlab path and try it out. (Note: If this function was to be used in a production environment you would, of course, add error handling and so forth.)

Code:
function [ output_args ] = blockSum( inputVec, blockSize)
%BLOCKSUM Summary of this function goes here
%
%   inputVec : A one-dimensional array containing your data.
%
%   blockSize: The size of the blocks you want to sum. For instance, if you
%              want to sum every ten successive elements in inputVec, you
%              would set blockSize = 10.% Define a cell array whose elements consist of slices of A of size
% blockSize.
C = num2cell(reshape(inputVec, blockSize, []), 1);

% Now sum the individual elements of the cell array C and return them as
% the output arguments of the function.
output_args = cellfun(@sum, C);

end

For instance, if we define a 1x40 array of the first forty integers, we can obtain the sums of blocks of ten numbers in this array as follows.

Code:
>> A = 1:40;
>> B = blockSum(A)

B =

     55   155   255   355

You can then access the individual elements of B as you would those of any other Matlab array.
 
I don't like extra m.files so I like:

blockSum = @(inputVec, blockSize) sum(reshape(inputVec, blockSize, []))';
 
And you just type that out every time you want to reuse the function?

Defining your own functions for such things is far superior since:

  • It allows you to handle errors predictably.
  • You can overload the function predictably.
  • You can generalize the function in a simply manner by using str2fun on a string argument.
  • Matlab is all about explicit functional operation on arrays. Not using functions and classes anywhere you can in Matlab is rather missing the point of the software.
  • Last, but not least, it saves a hell of a lot of time.
 
If its a rather simple and obscure one-line operation that I most likely won't have any use for besides that one script, then I do prefer using anonymous functions that way. It's still a function so I don't think your point number 4 really applies.
 
What was wrong with sum(reshape())?

Creating a new functions seems like a whole lot of fuss for this type of operation.