Help with writing a matlab code

Click For Summary

Discussion Overview

The discussion revolves around writing a MATLAB code to create a vector consisting of random-sized segments of zeros and ones, combined in a specific order without using loops. Participants explore different coding approaches and techniques.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant seeks a method to create a combined vector of zeros and ones without loops, specifying the need for random sizes for each segment.
  • Another participant provides a recursive function that generates the desired vector by alternating between appending zeros and ones based on a random integer, while also suggesting parameters for maximum iterations and segment lengths.
  • A different approach is mentioned, using a random vector to determine the values of zeros and ones based on comparisons to 0.5, which avoids recursion.
  • One participant expresses gratitude for the provided solution, indicating it may be helpful.
  • Another participant hints at a more efficient implementation but does not elaborate, suggesting the original poster explore further MATLAB resources for additional guidance.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the most efficient method, as one suggests an alternative approach without providing details, leaving the discussion open-ended regarding the best implementation.

Contextual Notes

The discussion includes assumptions about the maximum lengths and iterations for the recursive function, which may affect the output but are not explicitly defined by all participants.

alas
Messages
2
Reaction score
0
?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.
 
Physics news on Phys.org
Here is a recursive function that will do what you want:
Code:
[b]function[/b][/color] [/color][ v, i ] =  [/color]randomOnesZeros[/color](v, i)
    [/color]maxIter = 10; [i]% change this to what you want[/i][/color]
    maxLength = 10; [i]% change this to what you want[/i][/color]
    r = rand[/color]();
    r = mod[/color](floor[/color]((r *[/color] maxLength)), maxLength);
    [b]if[/b][/color] i[/color] <[/color]= maxIter
        [b]if[/b][/color] mod[/color](i[/color], 2)
            zeros[/color](1, r);
            v = [v zeros[/color](1, r)];
        [b]else[/b][/color]
            v = [v ones[/color](1, r)];
        [b]end[/b][/color]
        [v, i[/color]] = randomOnesZeros(v, i[/color] +[/color] 1);
    [b]end[/b][/color]
[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:
r = rand[/color](1, n);
vector = (r >[/color] 0.5) *[/color] zeros[/color](1, n) +[/color] (r <[/color] 0.5) *[/color] 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:
thank you, i really think it'll help me
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
Replies
5
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K