matlab - Using the if statement and for loop to write a program that generates N random numbers (and more) -
the question:
write matlab program that
i) generates n<=20 random numbers in interval [a,b], n, a, b entered via keyboard
my attempt:
a = input ('a=') b = input ('b=') n = input ('n=') n = (1:n) r = rand([a,b],[1,n]) end
doesn't seem work. following error messages appear
"??? subscript indices must either real positive integers or logicals."
what doing wrong?
ii) write numbers vector/array x
no idea how this? matter of putting r = x?
iii) write numbers divisible k screen, k entered via keyboard.
my attempt:
k = input ('k=') t = mod(x,k); x = i:n if mod(x,k) == 0 disp t end end
am anywhere near correct?
[i've never used stack-overflow before- having trouble formatting things appropriately] sorry
get n random numbers in range [a, b]:
a = input('a='); b = input('b='); n = input('n='); % floating point: r1 = + (b-a)*rand(1, n); % integers: r2 = round(a + (b-a)*rand(1, n));
as can see, r1 , r2 in vector form, so:
x = r1; % or x = r2;
for last part (this print duplicates well):
k = input('k='); divs_found = find(mod(r, k) == 0); disp(r(divs_found));
Comments
Post a Comment