Question

In: Computer Science

2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with...

2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with integer values ranging between −100 to +100. Using LOOP command, determine the number of negative elements in the vector. For example, given A = [15 −6 0 -8 −2 5 4 −10 -3 −5], the number of negative numbers are 6.

Solutions

Expert Solution

Program:

clc;

negativeCount = 0; % Initially negative count as 0

x = randi([-100 100],1,20); # Generate random numbers between -100 to 100

fprintf("The vector elements:\n");

for(i=1:length(x)) % Loop to print vector eleemnts and count negative elements
fprintf("%d\t",x(i)); % print vector eleemnts

if mod(i,5)==0 % mod operator print new line after 5 elements
fprintf("\n"); % print new line
end
  
if(x(i)<0) % check negative elements
negativeCount++; % Increment negative eleemnts count
end
end

fprintf("The number of negative elements are %d\n",negativeCount); % print negative eleemnts count

Output:


Related Solutions

Write a Matlab script that computes the element level stiffness matrix and force vector for linear...
Write a Matlab script that computes the element level stiffness matrix and force vector for linear elasticity.
Use Matlab and write entier script Problem 2. Generate the estimate (linear estimate) for y_est, and...
Use Matlab and write entier script Problem 2. Generate the estimate (linear estimate) for y_est, and plot(x , y_est). Use “hold on” and plot(x,y) and plot(x,y_est), so that you can see the result of the line and how it fits the data.   Does it look like it did a pretty good job? An estimate of the “residual error” is the sum of the squares of the difference between y and y_est. This is often denoted as r2. r2 = sum(...
In MatLab use the linspace function and the element-wise exponentiation (power) operator to generate the vector...
In MatLab use the linspace function and the element-wise exponentiation (power) operator to generate the vector 1, 10, 100, 1000, 10000 Please give the answer in form of linspace( , , )
MATLAB Write a script which asks the user of the program to provide an initial horizontal...
MATLAB Write a script which asks the user of the program to provide an initial horizontal position, initial vertical position, initial velocity, and angle. Create a time vector spanning from zero seconds to 100 seconds incremented at 0.01 seconds. Call the function that you created in the previous problem to calculate the trajectory and velocities of the projectile. Find the maximum height of the projectile and the time at which it reaches that point. Write a neat sentence stating what...
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...
MATLAB, if-else Write a script which to do the following. Take an array of any size...
MATLAB, if-else Write a script which to do the following. Take an array of any size as input and check if all the elements are integers. (i) If not, display the error message ‘Invalid input!’. Use the error command to create a proper error message in red. (ii) If yes, count the number of rows where the sum of the elements is smaller than the average row sum. It is recommended that you use a test variable with many rows...
Assume the vector AM contains an even number of elements. Develop a MATLAB script that asks...
Assume the vector AM contains an even number of elements. Develop a MATLAB script that asks the user for the vector AM and once the vector is found to contain an even number of elements, divide the product of its even elements by the sum of its odd elements without using the MATLAB functions prod or sum. Store the result in a variable called PDS. If AM is found to contain an odd number of elements, display an error message...
Write 2 MATLAB functions. One called exptrig which takes a vector of arbitrary length, x, containing...
Write 2 MATLAB functions. One called exptrig which takes a vector of arbitrary length, x, containing any real numbers, and computes the mathematical function f(x) = 3e^(-x)(cos^2(5x) - sin^2(5x)). The other one will be called trigsum, which takes a vector arbitrary length, x, containing any real numbers, and computes the mathematical function g(x)= ((sin(10x) + cos(10x))/ 2) + 3. Both functions will be computed at each of the values of x, and internally will output these values into a vector,...
write a matlab script for double mass spring system with animation.
write a matlab script for double mass spring system with animation.
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as...
Part A: Write a MATLAB script to find the volume of the cylinder in gallons, as well as the tank dimensions in feet. Assume that the initial measurements are 7 meters in diameter and 11 meters tall. Display your final answers to the screen using disp and a statement without a semicolon, e.g. write the following into your script disp(‘The capacity in U.S. gallons is:’), capacity, where capacity is a variable that you defined in preceding calculations. Part B: In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT