In: Computer Science
The following code must be written using matlab and must be using a for-loop. NOTE!
Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as positions 2, position7 drawn from same interval as positions 3, position8 drawn from same interval as positions 4, And then this should be repeated for these chunks of 4 along the whole length of the sequence. The length should be able to with multiples of 4 and should be given as a constant in the top of the script. Thanks
These intervals should be able to set by the user in the top of the script. Th
(*NOTE: Please up-vote. If any doubt, please let me know in the comments)
SCRIPT:
%Change the length below as required, it should be in multiples
of 4
length = 16;
%The following intervals will be used for random number
generation
%Interval format [minimum, maximum]
Int1 = [1,5];
Int2 = [7, 15];
Int3 = [20,30];
Int4 = [40,50];
%checking if lenght is multiple of 4, if not, stop execution
if mod(length,4) != 0
disp("lenght entered in script is not a multiple of 4, please
change & run again")
return
end
my_array = 1:length; %Initialize array of length
for n = 1:length
%Choosing interval based on the remainder of n with 4
if mod(n,4) == 1
my_array(n) = randi(Int1);
end
if mod(n,4) == 2
my_array(n) = randi(Int2);
end
if mod(n,4) == 3
my_array(n) = randi(Int3);
end
if mod(n,4) == 0
my_array(n) = randi(Int4);
end
end
disp(my_array)
TEST OUTPUT:
As we can see, the output vector satisfies the requirements.