How to Create a Routine in Matlab for Summing Every 10 Steps of a Matrix?

  • MATLAB
  • Thread starter kickapoo
  • Start date
  • Tags
    Code Matlab
In summary: What if you just wanted to append the sums into a new matrix?The code you provided to take the sum of a list of numbers is actually a really good way to do this. You could save the following code as sumMatrix.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.)function [ output_args ] = sumMatrix( inputVec, blockSize)%SUMMATORY Summary of this function goes here%% inputVec : A one-dimensional array containing your data.%% blockSize: The size of the blocks you want to
  • #1
kickapoo
5
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
  • #2
What have you tried?
 
  • #3
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 ?
 
  • #4
Like this?

sum(reshape(A,10,4))'
 
  • #5
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]
 
  • #6
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?
 
  • #7
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.
 
  • #8
I don't like extra m.files so I like:

blockSum = @(inputVec, blockSize) sum(reshape(inputVec, blockSize, []))';
 
  • #9
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.
 
  • #10
ty both for your time ..

you really help me a lot ...
 
  • #11
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.
 
  • #12
What was wrong with sum(reshape())?

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

Related to How to Create a Routine in Matlab for Summing Every 10 Steps of a Matrix?

1. What is Matlab?

Matlab is a programming language and interactive environment commonly used in scientific and engineering applications. It allows users to perform a variety of mathematical and data analysis tasks, as well as create visualizations and develop algorithms.

2. How do I write a simple code in Matlab?

To write a simple code in Matlab, you can open the Matlab software and click on the "New Script" button. This will open a blank file where you can type your code. Once you have written your code, you can run it by clicking on the "Run" button or by pressing the "F5" key on your keyboard.

3. What are variables in Matlab?

Variables in Matlab are containers for storing data values. They can hold numerical values, characters, strings, or arrays. Variables are created and assigned values using the equal sign (=) and can be used in calculations and operations within a code.

4. How do I plot a graph in Matlab?

To plot a graph in Matlab, you can use the "plot" function. This function takes in two vectors of equal length, one for the x-axis values and one for the y-axis values. You can also add labels, titles, and customize the appearance of the graph using various optional arguments.

5. Can I use conditionals and loops in Matlab?

Yes, Matlab supports conditionals (if/else statements) and loops (for and while loops) just like any other programming language. These are useful for controlling the flow of a program and repeating certain tasks based on certain conditions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top