Question

Solve in Matlab lang4. What change/changes can be made in the following code to make it more efficient? n = randi ([2 100]); for iv = 1:n inputnuuage

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#source code:

n=randi([2 100])
numvec=zeros(1,n)
tic
for iv=1:n
numvec(iv)=input('Enter a number: ')
end
toc
fprintf('The vector is:\n')
disp(numvec)

OUT AWNE n=randi([2 100]) numvec=zeros(1,n) tic for iv=1:n numvec(iv)=input(Enter a number: ); end toc fprintf(The vector

prezeros allocation can increase the speed of execution.

for loop must iterate n times so we can't modify the loop and within for loop two statements are present so

if n=20 with the statements need 2*20 so 40 times

so but make it one line we can used 20 times only

tic and toc are used ti display the time taken the program..

#if you have any doubt comment below...

Add a comment
Know the answer?
Add Answer to:
Solve in Matlab language 4. What change/changes can be made in the following code to make...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For the following M ATLAB code, What is the output of this code and many iterations...

    For the following M ATLAB code, What is the output of this code and many iterations will be performed if the Target entered is: Enter your target: 4? Enter your target: 5? Vector = -6:2:8; Target = input ('Enter your target:'). Position =0; for index = 1: length (Vector) if vector (index) == Target Position =Index; break end end if position == 0 disp ('The target was not found') else disp (['Position = ' num2str (Position)]) end

  • I am creating a MATLAB game for my school project. The goal of the game is...

    I am creating a MATLAB game for my school project. The goal of the game is to create a 'Treasure Hunt Game' that asks the user to input the number of players, the difficult (easy, medium, or hard), and asks the user(s) to pick spots on a matrix until the correct spot is chosen, therefore winning the game. If a player misses the spot, the command window doesn't show how far away the treasure is, but what direction it is...

  • Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code be...

    Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code below? %%Matlab code for Question 5. syms X y intlx exp(x),0,2); %integration of x*exp(x) fprintf("htlntegration of x"2*exp(x) for [O 3] is %2.5f.\n,y) %Integration using Trapizoidal rule nn [100 1000]; for ii 1:2 a:0; b-3; %Integration limit ns-1:nn(i) integral Values-zeros(size(ns)); ourFunction-@(x) x.*2.*exp(x); for n-ns val(n)-trapizoidal (ourFunction,a,b,nn(i); end fprintf nlntegration of x 2*exp(x) using Trapizoidal rule for [O 3]\n') fprintf('1tTrapizoidal integration value for n-%d...

  • Use the attached Matlab code as a basis to solve the following ordinary differential equation usi...

    Question 1 QUESTION 2 Use the attached Matlab code as a basis to solve the following ordinary differential equation using Euler's method, with timestep of 0.1, from t-0to t-100. d)0) -0 - sin (5vt cos(у Plot y versus t from t=0 to t=100. How many local maxima are on this interval(do not include end points). Be careful to count them all! Answer should be an integer 1 w% Matlab code for the solution of Module 2 3 dt-9.1; %dt is...

  • Change the following code to give only the first 2 picks; or 3 picks (Matlab) %...

    Change the following code to give only the first 2 picks; or 3 picks (Matlab) % colors for ball colors = ["red", "blue", "green", "yellow"]; % initial probabilites % change as required p = [0.20, 0.25, 0.35, 0.20]; % cumulative sum of p c = cumsum(p); % continue drawing until all are drawn while(size(p) > 0) % generate a random number r = rand(); % draw ball according to random number ball = 0; for i = 1:size(p, 2) if(r...

  • MATLAB ONLY!! PLEASE WRITE IN COMPUTER SO I CAN COPY PASTE!!! ANSWER COMPLETELY, USE FOR LOOPS....

    MATLAB ONLY!! PLEASE WRITE IN COMPUTER SO I CAN COPY PASTE!!! ANSWER COMPLETELY, USE FOR LOOPS. THE PROGRAM TO BE MODIFIED IS THE NEXT ONE: clc clear % Approximate the value of e ^ x using the Taylor Series x = input( 'Enter the value of x for e^x. x = ' ); t = input( 'Enter the amount of desired terms. t = ' ); i = 1; e_taylor = 0; % initializing the variable for the accumulator while...

  • Can you explain how this works? (cl) The following Matlab code is executed. What is Matlab's...

    Can you explain how this works? (cl) The following Matlab code is executed. What is Matlab's output? x - [0 1 2*ones(1, 4)]; b -2:3]. 2; y -x.*b; disp(y); Solution: 010 28 18

  • (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so...

    (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so that it implements the composilu trapezidul rulo, using a soquence of w -1,2, 4, 8, 16,... trapezoids, to approximatel d . This M file uses the MATLAB built-in function trapz. If 401) and y=() f(!)..... fr. 1)). 3= ($1, then the execution of z = trapz(x, y) approximates using the composite trapezoidal rule (with trapezoids). (Note that the entries of are labeled starting at...

  • I need a matlab code to answer the questions below ICE09B Make an Array Develop a...

    I need a matlab code to answer the questions below ICE09B Make an Array Develop a MATLAB code which will produce an array that looks like the following: 4 10 1. You must start with a blank array and build the array with a DNFL. You can NOT just load the array with an assignment statement. Hint: Use "addition" for your variables Square all values in the array 2. Blackboard will ask you for a screenshot of your properly working...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT