Code to copy:
%Define the function battleFormation().
function[V] = battleFormation(arr, inputFileName)
%Get the maximum value of the array arr.
maxVal = max(arr);
%Get the size of the vector arr.
[rowSize, vectorSize] = size(arr);
%Fill zeroes at odd positions.
oddArr = zeros(1, vectorSize);
%Fill zeroes in the array of vectorSize by maxVal.
reqArr = zeros(vectorSize, maxVal);
%Start a for loop till the size of the vector.
for index = 1 : vectorSize
%Get th eodd number position of the vector arr.
oddPos = mod(arr(index), 2);
%Assign this odd position value to the odd array at
%current index.
oddArr(index) = oddPos;
%End the for loop.
end
%Add the od position array values.
oddSum = sum(oddArr);
%If the sum of odd position values is not equal to the
%size of the vector.
if oddSum ~= vectorSize
%Display an error message and return from the
%function.
error('Error! Please enter odd values.');
return;
end
%Start a for loop again till the vector size.
for index = 1 : vectorSize
%Get the difference of current value from maximum
%value.
elem = maxVal - arr(index);
%Divide the required element by 2.
b = elem/2;
%Start another for loop from b + 1 to the maxVal –
%b.
for k = b + 1 : maxVal - b
%Assign 1 to the element of require array.
reqArr(index, k) = 1;
%End inner for loop.
end
%End outer for loop.
end
%Open the file name in write mode.
infile = fopen(inputFileName, 'w');
%Start another for loop till the vector size.
for index = 1 : vectorSize
%Form anther vector using reqArr.
reqVector = reqArr(index, :);
%Display the current line number with required
%formations on the file.
fprintf(infile, "\n%d| Line %d has the following line up: ", index, index);
%Start a for loop till the maxVal.
for reqIndex = 1 : maxVal
%Display the required vector.
fprintf(infile, "%d ", reqVector(reqIndex));
%End the inner for loop.
end
%End the outer for loop.
end
%Close the input file.
fclose(infile);
%Use type() command over input file.
type(inputFileName);
%Use save() command over input file name.
save(inputFileName);
%End the function.
end
matlab Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number...
matlab Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number of soldiers each line 2. (char)A file name, indluding extension, for the output file group number Formation1.txt: 1 Line 1 has the following line up: e 1 1 1 0 File Outputs: 21 Line 2 has the folloOwing line up: 1 1 1 1 1 1. A text file showing the soldiers formations 3 Line 3 has the following line up: e e 1...
Thank you for your help! Homework 02 - Vectors, Strings, and Masking Function Name: badWeatherman Inputs: 1. (logical) Vector of suspect #1's answers to a lie detector (logical) Vector of suspect #2's answers to a lie detector (logical) Vector of suspect #3's answers to a lie detector (logical) Vector of suspect #4's answers to a lie detector 2" 3. 4. Outputs 1. (char) Sentence stating which suspect stole the fruit Banned Functions: isequal (, isequaln( Background Oh no, there has...
MATLAB QUESTION Write a MATLAB function called vector_input that takes no inputs and returns no outputs. Instead, when called, it gives control to the user and asks the user to input a length-3 vector. Then the function prints: The sum of ___, ____, and ____ is ____. [new line] where the first three blanks are filled by each element in input vector, and the last blank is the sum of all three elements.
Using MATLAB, please read question well Function Name: stPaddysFeast Example search- Inputs 1 (struct) A MxN structure array 2. double) A 1x2 vector of your starting position: [row, column] next: [1, 3] next: [1, 1] next: [2, 1) food: 'potato food potato next: 2, 3] step 2 food: start next: [2, 2] food: 'potato' step 3 Outputs next [1, 21 food: food: 1. (ohar)A descriplion of how mary potatoes you found ad where you stopped looking step 4 (stop) Background...
There is a file called mat2.txt in our files area under the Assignments folder. Download it and save it in the same folder where this Matlab file (HW08_02.m) is saved. It may be worth opening that text file to see how it is set out. Note well, however, that the file might have a different number of lines and different number of numbers on each line. Write a Matlab program that does the following: Prompt the user for an input...
MATLAB code help! Function Name: chemTimer Inputs: 1. (double) The current position of the hour hand 2. (double) The current position of the minute hand 3. (double) A positive or negative number of minutes Outputs: 1. (double) The position of the hour hand after the specified time 2. (double) The position of the minute hand after the specified time Background: Oh no, you were running late to your Chem lab and completely forgot your reaction timer! It's a good thing...
MATLAB code help! Function Name: jackSparrow Inputs: 1. (double) A 1x2 vector describing your hand 2. (double) A 1x2 vector describing Jack Sparrow's hand 3. (double) A vector with the next cards to be drawn from the deck Outputs: 1. (char) A description of the game outcome Background: You're out at sea with the infamous Jack Sparrow, and it's a pretty calm day. Without anything better to do, Jack Sparrow challenges you to his favorite card game: BlackJack (Sparrow edition)...
Function Name: zoomZoom Inputs: 1. (double) An Nx5 array of scooters' speeds at various times 2. (double) The time at which you want to find the scooters' speeds Outputs: 1. (double) A 1x5 vector of scooters' speeds at the given time Background: Scooter go zoom zoom! Function Descripti on : You will be given an array with times in the first column, and speeds of different electric scooters in columns 2-5 that correspond to those times. Use spline interpolation or...
Function Name: sepDigits Inputs: 1. (double) A three-digit integer Outputs: 1. (double) The hundreds-place digit of the input 2. (double) The tens-place digit of the input 3. (double) The ones-place digit of the input Function Description: This function will take in a three-digit integer (integer between 100 and 999 , inclusive) and should return each digit as a separate number. For example, if the input is 472 , the first output would be 4 , the second would be 7...
matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...