0

I wanna randomly select 1 by 6 matrix elements between -1 and 1.

For example, [-1 1 -1 1 1 1]

How do I select it??

1

2 Answers 2

2

Create a vector of possible values, and use randi to generate random indices into that vector.

Choices = [-1 1];
Index = randi(length(Choices), 1, 6);
Choices(Index)
ans =

     1     1     1    -1     1    -1
Sign up to request clarification or add additional context in comments.

2 Comments

Read up on random numbers in matlab; you'll need to seed the random number generator if you need it to be different every time you start up the program.
+1! I have always used a combination of round and rand for this type of thing in the past, but this is much better!
0

Just for the sake of completeness:

randVec = 2*randi([0 1], 1,6) - 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.