Reputation: 131
The question:
Write a matlab program that
i) generates N<=20 random numbers in the interval [a,b], where N, a, b are entered via keyboard
My attempt:
a = input ('a=')
b = input ('b=')
N = input ('N=')
for N = (1:N)
r = rand([a,b],[1,N])
end
doesn't seem to work. The following error messages appear
"??? Subscript indices must either be real positive integers or logicals."
What am I doing wrong?
ii) write the numbers to a vector/array x
NO idea how to do this? Is it simply a matter of putting r = x?
iii) write all the numbers divisible by k to the screen, k is entered via the keyboard.
My attempt:
k = input ('k=')
t = mod(x,k);
for x = i:N
if mod(x,k) == 0
disp t
end
end
Am I anywhere near correct?
[I've never used stack-overflow before- having trouble formatting things appropriately] Sorry
Upvotes: 0
Views: 735
Reputation: 463
Get N random numbers in range [a, b]:
a = input('a=');
b = input('b=');
N = input('N=');
% For floating point:
r1 = a + (b-a)*rand(1, N);
% For integers:
r2 = round(a + (b-a)*rand(1, N));
As you can see, r1 and r2 are already in vector form, so:
x = r1;
% or
x = r2;
For the last part (this will print duplicates as well):
k = input('k=');
divs_found = find(mod(r, k) == 0);
disp(r(divs_found));
Upvotes: 2