Question

In: Computer Science

The following code must be written using matlab and must be using a for-loop. NOTE! Write...

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

Solutions

Expert Solution

(*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.


Related Solutions

The following code must be written using matlab and must be using a for-loop. NOTE! Write...
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 in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only...
How to design FIR lowpass filter using matlab. Note : Do not write matlab code. Only explain the steps of designing filter
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Write a MATLAB code to calculate the electricity usage in a residential home. Note: Thing that...
Write a MATLAB code to calculate the electricity usage in a residential home. Note: Thing that impact the charge could be number of rooms, square feet, electric appliances. Plot the electricity charge in terms of the variability.
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
write a matlab code to find the following: initial position, initial velocity, and acceleration using the...
write a matlab code to find the following: initial position, initial velocity, and acceleration using the algorithm and information below time(seconds). height(m) velocity(m/s) 0. 0.2. 2.95 algorithm: 1. Enter data in to arrays. 2. Fit the height data to a 2nd order polynomial. 3. Evaluate the polynomial at enough points to get a smooth curve. 4. Find the velocity model by taking derivative of the height polynomial. 5. Evaluate the velocity polynomial at enough times to get a smooth curve
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT