Question

In: Computer Science

Matlab For a vector x=[1,10,-5,72,56,70,33] a) Use for loop to sum the vector x and count...

Matlab

For a vector x=[1,10,-5,72,56,70,33] a) Use for loop to sum the vector x and count how many values are greater than 33. b) Repeat (a), this time a while loops.

Solutions

Expert Solution

code:

x=[1,10,-5,72,56,70,33]
sum = 0
count = 0
%doing with for loop
for i = 1:length(x)
%adding to variable sum
sum+=x(i)
%checking if number is grater than 33
if x(i)>33
%increment the count
count++
end
end

fprintf("Sum of values : %d",sum)
fprintf("\nTotal %d numbers are greater than 33",count)

code:

x=[1,10,-5,72,56,70,33]
sum = 0
count = 0
%doing with while loop
i=1
while i<=length(x)
sum+=x(i)
if x(i)>33
count++
end
%incrementing loop variable
i++
end
fprintf("Sum of values : %d",sum)
fprintf("\nTotal %d numbers are greater than 33",count)


Related Solutions

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
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate...
IN MATLAB, Create a 8x8 magic matrix. Then use MATLAB built in function, sum, to calculate the sum of each column. Use subscript notation to find the 6th element and the last element of each magic square. Find the maximum of all the elements in each magic square.
Use Matlab to introduce arow vector,f,and define it as a 1 by 4 vector showing values...
Use Matlab to introduce arow vector,f,and define it as a 1 by 4 vector showing values of 1, 2, 3and 4. Now try to build matrix D, using the following 2 commands a.D = [f ; A] b.D= [f , A] c.Which one can be built,and which one cannot be built? Explain your answer. Matrix A, which is a 4by 4 matrix, (The first, second, third and fourth column values are:2,4,6,8and 1,3,5,7 and 8,6,4,2 and 7,5,3,1 , respectively.
MATLAB. A three vector force system has forces as outlined, use a three element vector to...
MATLAB. A three vector force system has forces as outlined, use a three element vector to represent the x, y, and z terms respectively, F = [x_value, y_value, z_value]. Find the resultant force when: F1 has a magnitude of 120 N and is in the x-y plane, 35 degrees BELOW the positive x axis, F2 has a magnitude of 810 N and is in the x-z plane, 100 degrees ABOVE the positive x axis, and F3 has a magnitude of...
Use the cumsum command in MATLAB to show that the cumulative sum of an impulse is...
Use the cumsum command in MATLAB to show that the cumulative sum of an impulse is a unit step function. Add a line to your code from Problem 2. Plot the unit step function generated by the cumsum command. Turn in a copy of the code and graph.
Matlab question: By using while loop to solve, Sum up bottles until you are over 75....
Matlab question: By using while loop to solve, Sum up bottles until you are over 75. How many days/loops are needed? Print out cups on the shelf and total until you reach at least 75. Finish the code below, least_num_Cups = randi([75, 300], 1,1); while totalCups = % Change this code to reflect change in totalCups fprintf('%0.0f cups of tea on the shelf, total %0.0f\n',); k = ; % Change code to update k end k_complete = ; % Change...
Use MATLAB to create a vector x having six values between 0 and 10 (including the endpoints 0 and 10
Use MATLAB to create a vector x having six values between 0 and 10 (including the endpoints 0 and 10). Create an array A whose rst row contains the values 3x and whose second row contains the values 5x - 20.  
In MatLab 1. Ask the user to enter a 1 x 5 vector of numbers. Determine...
In MatLab 1. Ask the user to enter a 1 x 5 vector of numbers. Determine the size of the user entry. 2. Ask the user to enter a 1 x 5 vector of numbers. Determine the size of the user entry. If the user enters a 5 x 1 vector, use a warning statement to inform the user of their error, and set the new input value to be the transpose of the user input value. If the user...
Write a MATLAB function that accepts input vector x and returns the number of local maximums...
Write a MATLAB function that accepts input vector x and returns the number of local maximums of x with value between xmin and xmax. Ask user to input values xmin and xmax at the beginning of the procedure. Use vector x (the vector x from that file consists of 1000 numbers ranging from 0.0044 to 0.67).
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop...
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop index Can use a non-unit stride Are required for any kind of animations An accumulator: MUST start at zero Can be used to keep track of a quantity each time through the loop Can be used to calculate how a sum changes with each loop Should be coded, for loops are more efficient For loops: Perform a set task a set number of times...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT