Question

Create a script that receives any row matrix with positive integer elements as the input (suppose...

Create a script that receives any row matrix with positive integer elements as the input (suppose you have at least two elements in your matrix), and creates the output as a row matrix with only two elements, where the first element will be sum of all the oddly placed element of the input, and the second value will represent the sum of all the evenly placed elements of the input: Example: A = [11 , 4 , 42 , 31 , 1] B=output = [54 , 35]

by matlab funtions

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

CODE :

%This function takes a row matrix as input. It calculates sum of

%even and odd positioned elements and returns them

%in a 1x2 matrix

function A = matrixOp(inputArray)

%create variables to store sum at even and odd positions

even_sum = 0;

odd_sum = 0;

for i = 1: length(inputArray) %loop through elements of input matrix    

   if (mod(i,2)== 0) %even position

       even_sum = even_sum + inputArray(i);

   else

       odd_sum = odd_sum + inputArray(i);

   end

end

   

%create answer array

A = [odd_sum, even_sum];     

end

Add a comment
Know the answer?
Add Answer to:
Create a script that receives any row matrix with positive integer elements as the input (suppose...
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
  • Write a function that receives a positive integer as input and returns as output a vector...

    Write a function that receives a positive integer as input and returns as output a vector containing all factors of the given integer. MATLAB

  • Without using the factorial command, create a code that receives a positive integer as the input,...

    Without using the factorial command, create a code that receives a positive integer as the input, and prints the reciprocal of its Factorial as the output Example: ?=4→??????=, -!= , -×0×1×,=, 1-=0.0417 Example: ?=2→??????=, 1!=, 1×,=, 1=0.5

  • To be answered in the form of a Matlab code: Write a code that finds the...

    To be answered in the form of a Matlab code: Write a code that finds the sum of all the positive elements in the input received by the computer as a row matrix (assume all the elements are integers, and there exists at least one positive integer among the input values): Example: ? = [1, -4, 12, -51, -1]                   ? = ?????? = 13

  • In Matlab write a script that does the following. 1. Creates a row vector v =...

    In Matlab write a script that does the following. 1. Creates a row vector v = [3, 6, 9, 9, 15], and then manipulates v to create each of the following vectors (and then displays the result of each): (a) a = [9, 36, 81, 81, 225] (b) b = [ 1 3 , 1 6 , 1 9 , 1 9 , 1 15 ] (c) c = [1, 2, 3, 3, 5] (d) d = [15, 9, 9,...

  • Problem 5 Create the following matrix by typing elements explicitly one command. Do not type individual...

    Problem 5 Create the following matrix by typing elements explicitly one command. Do not type individual 0 00 0 0 o o 0 0 0 0 0 1 2 3 F=0 01 10 20 0 0 2 8 26 0 0 3 6 32 E 0 045 6 0 0 7 8 9 Problem 6 Create two row vectors: a -4 10 0.5 1.8 -2.3 7, b [0.7 9 -53-0.6 12 (a) Use the two vectors in a MATLAB command...

  • Java problem In this problem, the first input is a positive integer called n that will...

    Java problem In this problem, the first input is a positive integer called n that will represent the number of lines to process. The lines to be processed have one or more integers separated by whitespaces. For each of these lines, you must output: The minimum value of the integers The maximum value of the integers The sum of the integers It is worth to mention that the number of integers of each line is not known a priori and...

  • In MATLAB, ask the user to input a matrix of any size. Ensure that the user...

    In MATLAB, ask the user to input a matrix of any size. Ensure that the user inputs a positive, integer matrix. If there is an incorrect input, ask them for a matrix again. Use Loops to do this. Using loops, find the sum of all prime numbers present in the user-entered matrix. You must not use the built-in isprime function to do this. Use loops. (The number 1 may count as as a prime number) Example Matrix: [1 2 3;...

  • *** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...

    *** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an nby- n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note...

  • MATLAB Homework 3- Due:5/20/2018 1. Create a function that receives the following input data, and prints...

    MATLAB Homework 3- Due:5/20/2018 1. Create a function that receives the following input data, and prints the output as the summation of the diagonal elements (The elements which are located at the intersection of similarly numbered rows and columns) Input (Matrix A) Output (Scalar B) Example 2 -1 4 3 -45 ? B = A( 1,1) + A(2,2) = 2-4 =-2 A4 5 ?41 ? B = A(1,1) + A(2,2)--1-4-5 -3 1 1.5 -4 3.2 -1 06 1.78 2.22 -4.56...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

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