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

Help with writing a matlab code

  1. Feb 4, 2012 #1
    ?hi,
    im trying to write a code that should do this-
    create a random sized zeros vector
    create a random size ones vector
    i have to do this several times and combine it all(by that order) into one vector that should lookz like-
    0000011111001111001...
    does anybosy have an idea how to do it without using any loop?

    thanks.
     
  2. jcsd
  3. Feb 4, 2012 #2

    jhae2.718

    User Avatar
    Gold Member

    Here is a recursive function that will do what you want:
    Code (Text):

    [color=#008000][b]function[/b][/color][color=#bbbbbb] [/color][ v, i ] = [color=#bbbbbb] [/color][color=#0000FF]randomOnesZeros[/color](v, i)[color=#bbbbbb]
        [/color]maxIter = 10; [color=#408080][i]% change this to what you want[/i][/color]
        maxLength = 10; [color=#408080][i]% change this to what you want[/i][/color]
        r = [color=#008000]rand[/color]();
        r = [color=#008000]mod[/color]([color=#008000]floor[/color]((r [color=#666666]*[/color] maxLength)), maxLength);
        [color=#008000][b]if[/b][/color] [color=#008000]i[/color] [color=#666666]<[/color]= maxIter
            [color=#008000][b]if[/b][/color] [color=#008000]mod[/color]([color=#008000]i[/color], 2)
                [color=#008000]zeros[/color](1, r);
                v = [v [color=#008000]zeros[/color](1, r)];
            [color=#008000][b]else[/b][/color]
                v = [v [color=#008000]ones[/color](1, r)];
            [color=#008000][b]end[/b][/color]
            [v, [color=#008000]i[/color]] = randomOnesZeros(v, [color=#008000]i[/color] [color=#666666]+[/color] 1);
        [color=#008000][b]end[/b][/color]
    [color=#008000][b]end[/b][/color]
     
    In this function, we have two variables, [itex]\mathbf{v}[/itex] and [itex]i[/itex]. [itex]\mathbf{v}[/itex] is your vector and [itex]i[/itex] is a counter for the number of iterations. We set a value for the max length of zeros or ones to add, and a maximum number of iterations for the function. Then, we compute a random integer [itex]r \in [0, {\tt maxLength}][/itex].

    If the iteration is even, we append a [itex]1 \times r[/itex] vector of zeros to our vector, otherwise we append a [itex]1 \times r[/itex] vector of ones to our vector.

    Then, if we are below the maximum number of iterations, we recursively call our function, making sure to save the return value in the current scope.

    For just a random vector of ones and zeros, you could use a statement like:
    Code (Text):

    r = [color=#008000]rand[/color](1, n);
    vector = (r [color=#666666]>[/color] 0.5) [color=#666666]*[/color] [color=#008000]zeros[/color](1, n) [color=#666666]+[/color] (r [color=#666666]<[/color] 0.5) [color=#666666]*[/color] [color=#008000]ones[/color](1, n);
     
    This creates a vector [itex]\mathbf{r} = \langle r_1, r_2, \ldots , r_n\rangle[/itex] of random values s.t. [itex]r_i \in [0,1][/itex]. In the next statement, we compare an element of the vector to the value of the center of the interval, 0.5. If it's larger than 0.5, we set the element to 0 and if it's smaller than 0.5 we set that value to 1.
     
    Last edited: Feb 4, 2012
  4. Feb 5, 2012 #3
    thank you, i really think it'll help me
     
  5. Feb 5, 2012 #4

    jhae2.718

    User Avatar
    Gold Member

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help with writing a matlab code
  1. Matlab Coding Help ! (Replies: 3)

  2. Matlab code help (Replies: 5)

Loading...