Help with writing a matlab code

In summary, the conversation discusses a code that creates a random vector with both zeros and ones, without using any loop. A recursive function is suggested, and the code is provided along with some tips for more efficient implementation and helpful MATLAB resources.
  • #1
alas
2
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
  • #2
Here is a recursive function that will do what you want:
Code:
[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:
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:
  • #3
thank you, i really think it'll help me
 
  • #5


I would recommend breaking down your problem into smaller steps and using built-in functions in MATLAB to achieve your desired outcome.

First, you can use the "randi" function to create a random sized vector of zeros and ones. For example, you can use "randi([0,1],1,10)" to create a vector of 10 zeros and ones.

Next, you can use the "repmat" function to repeat this vector multiple times. For example, you can use "repmat(randi([0,1],1,10),1,3)" to create a vector with three repetitions of the initial vector.

Finally, you can use the "horzcat" function to combine these vectors horizontally into one vector. For example, you can use "horzcat(repmat(randi([0,1],1,10),1,3),repmat(randi([0,1],1,10),1,2))" to create a vector with five repetitions of the initial vector in the desired order.

By using these built-in functions, you can avoid using any loops in your code. I hope this helps and good luck with your MATLAB coding!
 

1. How can I write a basic matlab code?

To write a basic matlab code, you can start by opening the matlab software and creating a new script file. Then, you can start writing your code in the script file using the matlab syntax and functions. Don't forget to save your code file after you finish writing it.

2. How do I debug my matlab code?

To debug your matlab code, you can use the built-in debugging tools such as the debugger window or the breakpoints. You can also use the "dbstop" command to pause the code at a specific line and check the values of variables. Additionally, you can use the "disp" function to display the values of variables at different points in your code.

3. How can I optimize my matlab code?

To optimize your matlab code, you can use vectorization and avoid using for loops as much as possible. You can also preallocate arrays and variables to avoid unnecessary memory usage. Additionally, you can use built-in functions and expressions instead of creating your own, as they are usually optimized for faster execution.

4. How can I read data from a file in matlab?

To read data from a file in matlab, you can use the "readtable" or "csvread" functions for tabular or csv files, respectively. For other file types, you can use the "fopen" and "fscanf" functions to open and read from the file. Make sure to specify the correct file path and format in your code.

5. How do I plot data in matlab?

To plot data in matlab, you can use the "plot" function. You can specify the x and y values as well as add labels, titles, and customize the appearance of the plot. You can also use other plot types such as scatter plots, histograms, and bar graphs using the appropriate matlab functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
818
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
Back
Top