Reputation: 37
I have to create this first script that can later be used by different 1 x N
arrays. How would I code a 1 x N
array function so in later use can include elements either 1 or 0 (logical or double).
As an example :
V = [1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1];
So I can later test out code for many different values and lengths of V.
TEST CASE 1-------------------------------------------------------------------------------
[V] = lab3p2partA([1 0 1 0 1 0 0 0])
V =
1 0 0 0 1 1 0 0
Upvotes: 0
Views: 155
Reputation: 104484
If V
is already given, simply do:
V = V == 1;
... to turn V
into a logical
array.
However, if your intent is to generate a random array, you can use randi
to generate a random integer array:
V = randi(2, N, 1) - 1;
You generate numbers of 1 or 2, then subtract by 1. randi
's minimum value to generate is 1 and not 0 unfortunately.
You can also use rand
with thresholding as the other post suggests:
V = rand(N, 1) > 0.5;
Make sure you choose 0.5 because rand
generates things uniformly. rand
generates values from [0,1]
uniformly, so if you want (on average) an even number of 0s and 1s, set your threshold to 0.5.
Upvotes: 0
Reputation: 81
Not sure if it is sufficient for you, but you could do something like this:
N = 15; %to be changed to the desired length
V = rand(1,N)>.3 %Increase number for less ones, decrease for more.
Upvotes: 1