how to change the range without typing in each time?

3 views (last 30 days)
hi is it possible to make it like, i need to draw samples from 0 to 100. however, i cannot type in the desire range. for example i need to draw sample from 0 to 10, 11 to 20 and so on, but i cannot type in each time to change the range. how can i make a code that will help me to run it without having to type in each time? is it possible?
if true
for k = 0:9;
n(k) =(11*k : 10*(k+1)); %range
a = randsample(n,5);
end
% code
end
the above code has error. is it possible to even use for loop?
  1 Comment
Stephen23
Stephen23 on 25 Mar 2015
I notice you have asked multiple questions on MATLAB Answers, but do not accept any of them. It is considered polite on this forum to accept answers that help resolve your problem.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 25 Mar 2015
Edited: Stephen23 on 26 Mar 2015
It is better if you give us exact descriptions and examples of your inputs and outputs, and broken code does not actually tell us what you want to achieve and we yet can't read minds. This might help you for future questions:
Broken code does not really help because we already have a good idea of what MATLAB can do, what we don't know is what you want to do: this is where examples are very useful, and they also provide use with something to test our solutions on.
It seems that you want to generate a vector of ten random integers between one and one hundred, where each value is from a separate range: 1:10, 11:20, etc. Note that you specified zero to 100, but this means the first range has 11 values and all other ranges would have 10 values: I gave solutions for both of these.
You really should learn about how to write vectorized code in MATLAB rather than doing everything in loops: vectorized code is neater and faster.
We can generate all of the random values at once using randi and the colon operator, like this:
>> randi(10,1,10) + (0:10:90)
ans =
1 13 29 31 50 58 65 76 83 95
>> randi(10,1,10) + (0:10:90)
ans =
10 16 26 33 45 57 67 74 84 100
which generates a vector of random integers, each value from a range of ten possible values: 1:10, 11:20, etc. The two examples above show the highest and lowest possible values. Note that the parentheses around the vector are significant, and should not be removed.
If you really do want the first group to start with zero, even though this would mean that it has eleven elements, then this code will achieve this:
>> [randi(11)-1, randi(10,1,9) + (10:10:90)]
ans =
10 17 21 39 50 57 68 78 84 97
>> [randi(11)-1, randi(10,1,9) + (10:10:90)]
ans =
0 15 24 38 48 52 65 75 87 98
  3 Comments
ker fang fang
ker fang fang on 25 Mar 2015
oh, i figure it out already, randi(10,1,10) is choosing 10 random values, making the size 1 by 10. is it correct? thank you so much.
Stephen23
Stephen23 on 25 Mar 2015
Edited: Stephen23 on 25 Mar 2015
A tip for beginners: the f9 button will run any highlighted code or file. For example, if you have this line in your command window, or in a function or script:
randi(10,1,10) + (0:10:90)
then you can select any part of it (e.g. the (0:10:90) part) and press the f9 button: this will execute the selected code. When we try it with the two parts of this code, this is what we will find displayed in the command window:
>> randi(10,1,10)
ans =
6 2 3 9 1 5 2 10 8 6
>> (0:10:90)
ans =
0 10 20 30 40 50 60 70 80 90
So we can see that randi call has generated a 1x10 vector of random integers between one and ten. The (0:10:90) call has generated a 1x10 vector of integers [0,10,20...,90]. We then simply add them using the plus sign to get the final vector of random values. Try changing the values and see what happens! I promise you, your computer will not break.
Please read the links that I gave in my answer: I did not put them there just to make the page more colorful, but for you to read. They will tell you how these bits of code work. You will find that MATLAB's documentation is rather good, and it is worth reading, understanding and using.
As you still seem to be confused by basic MATLAB code, you should work through these tutorials:

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!