Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple Matlab question code

  1. Apr 22, 2010 #1
    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.

  2. jcsd
  3. Apr 22, 2010 #2
    What have you tried?
  4. Apr 22, 2010 #3
    I am trying something but as a noob in matlab i am stuck

    %a=[1 1 1 1 1 2 2 2 2 2]'
    %a1 klimaka

    for idx=[a0+k]
    for idx=[a1+k]

    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 ?
  5. Apr 23, 2010 #4
    Like this?

  6. Apr 23, 2010 #5
    ty for time.. reshape dont 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

    for n=1:length(a)/k;

    steps=[sum_new mean_new max_new min_new]
  7. Apr 23, 2010 #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?
  8. Apr 23, 2010 #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 (Text):

    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);

    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 (Text):

    >> 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.
  9. Apr 23, 2010 #8
    I don't like extra m.files so I like:

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

    you really help me a lot ...
  12. Apr 23, 2010 #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.
  13. Apr 26, 2010 #12
    What was wrong with sum(reshape())?

    Creating a new functions seems like a whole lot of fuss for this type of operation.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Simple Matlab question code
  1. MATLAB simple code (Replies: 4)