Solving MathLab Question - Vector of Zeros & Ones

  • Thread starter GreenPrint
  • Start date
In summary, the conversation is about creating a function in MatLab that uses a given probability to change the values in a given vector. The function uses the rand function to generate a random number and checks if it is less than or equal to the given probability. If it is, the value in the vector is flipped from 0 to 1, or from 1 to 0. The function loops over all elements in the vector, changing them based on the probability, and returns the modified vector as the result.
  • #1
GreenPrint
1,196
0
MatLab Question

Given a vector of values of zeros and ones such as [1 0 1 0 0 0 1 0] I'm asked to, given a probability provided by the user, use that probability to change the values in the vector using that probability.

For example given the vector [0 1 0 0 0 1 0 0 0 1] and probability .2 provided by the user there is a 20% chance that the values in the vector will change (either from 0 to 1 or 1 to 0) or that there's a good chance that a vector of 10 values 2 values in that vector will change.

I'm asked to create a function to do this and kind of confused how to do this with the probability of changing the values (apparently I have to use rand function which I know how to use just kind of lost how to do this)

I assume that I would have to take each value within the vector (there will be exactly 8 values in the vector) and doing something to each of these values with the probability and changing them from the current value to the one it is not (either 1 or 0).

I'm kind of lost. Thanks for any help which you can provide.
 
Last edited:
Physics news on Phys.org
  • #2
Solution: function result = myProb(vector,prob) result = vector; % Initialize the result vector for i=1:length(vector) % loop over all elements in vector if rand() <= prob % Generate a random number and check if it is less than or equal to the given probability result(i) = mod(vector(i)+1,2); % If it is less, flip the value from 0 to 1, or from 1 to 0 end endend
 

FAQ: Solving MathLab Question - Vector of Zeros & Ones

What is a vector of zeros and ones in MathLab?

A vector of zeros and ones in MathLab is a special type of vector that contains only 0s and 1s as its elements. It is commonly used in coding and data analysis to represent binary data or boolean values.

How do I create a vector of zeros and ones in MathLab?

To create a vector of zeros and ones in MathLab, you can use the zeros and ones functions. These functions take in the desired size of the vector as an argument and will return a vector filled with either 0s or 1s, respectively.

Can I change the elements of a vector of zeros and ones in MathLab?

Yes, you can change the elements of a vector of zeros and ones in MathLab. You can use indexing to access specific elements and assign new values to them. However, the size of the vector cannot be changed once it is created.

What are some common applications of a vector of zeros and ones in MathLab?

A vector of zeros and ones is commonly used in image processing, where it is used to represent black and white images. It is also used in machine learning and data analysis to represent categorical data or boolean variables. Additionally, it can be used in coding to represent binary data or perform logical operations.

Are there any alternative ways to create a vector of zeros and ones in MathLab?

Yes, there are alternative ways to create a vector of zeros and ones in MathLab. You can use the repmat function to replicate a single element multiple times, or you can use the sparse function to create a sparse vector of zeros and ones. You can also manually create a vector using indexing and assign 0s and 1s to specific elements.

Similar threads

Replies
3
Views
772
Replies
17
Views
1K
Replies
3
Views
1K
Replies
18
Views
3K
Replies
6
Views
4K
Replies
3
Views
2K
Back
Top