Question

In: Computer Science

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 (> 10 ).

Solutions

Expert Solution

Case 1 :

When the matrix contains elements that are not integers

MATLAB code :

A = rand(11,11)
[rows,columns] = size(A);
row_sum = sum(A,2);
total = sum(row_sum);
avg_row_sum = total / rows;

count = 0;
for i = 1:rows
    sum = 0;
    for j = 1:columns
        if floor(A(i,j)) ~= A(i,j)
            error('Invalid Input')
            return
        else
            sum = sum + A(i,j);
        end
    end
    if sum < avg_row_sum
        count = count + 1;
    end
end
disp(['The number of rows with sum smaller than average is : ', num2str(count)])

Output :

.

Case 2 :

When the matrix contains elements that are integers

MATLAB code :

A = randi([1,10],[11,11])
[rows,columns] = size(A);
row_sum = sum(A,2);
total = sum(row_sum);
avg_row_sum = total / rows;

count = 0;
for i = 1:rows
    sum = 0;
    for j = 1:columns
        if floor(A(i,j)) ~= A(i,j)
            error('Invalid Input')
            return
        else
            sum = sum + A(i,j);
        end
    end
    if sum < avg_row_sum
        count = count + 1;
    end
end
disp(['The number of rows with sum smaller than average is : ', num2str(count)])

Output :

.

.


Related Solutions

Write a function script DirCos.m that takes a vector (any row or column array) as the...
Write a function script DirCos.m that takes a vector (any row or column array) as the argument and returns the direction cosines for that vector. This is for a MatLab script
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2 – K) S(n) – S(n-1) ;   Assume S(1) = 0, S(2) = 1; (a)    Case 1, Assume K = 1 (b)    Case 2, Assume K = 2 (c)     Case 3, Assume K = 4 Plot all of these on the same plot, for N = 1 to 20
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...
Write a MATLAB script file to integrate  using trapezoid method (do not use trapz command for this...
Write a MATLAB script file to integrate  using trapezoid method (do not use trapz command for this part, write your own script). Consider x=-0.5 to 3 with Δt=0.01, and compare your result with using “integral” and "trapz" commands (all in one file).
Write a MATLAB script file to numerically solve any first order initial value problem using Rulers...
Write a MATLAB script file to numerically solve any first order initial value problem using Rulers method. Once code is working use it to solve the mixing tank problem below. Use a step size of 1 minute, and simulate the solution until the tank contains no more salt. Plot both the Euler approximation and the exact solution on the same set of axes. A tank contains 100 gallons of fresh water. At t=0 minutes, a solution containing 1 lb/gal of...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
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...
Write MATLAB script programs to perform the following conversions, taking a value in SI units as...
Write MATLAB script programs to perform the following conversions, taking a value in SI units as the input argument and returning the value to US Customary Units. a. Length: Centimeters to inches b. Temperature: °C to °F c. Force: Newton to Pound-force d. Speed: Meters per second to miles per hour Write MATLAB functions to perform the following conversions, taking a value in SI units as the input argument and returning the value to US Customary Units. a. Length: Centimeters...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this new matrix to a file called “Assignment3_Question5.dat”.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT