Question

MATLAB function Write an efficient function, f(v) that takes a one-dimensional array of length N^2 for...

MATLAB function

Write an efficient function, f(v) that takes a one-dimensional array of length N^2 for a square matrix (in a row-vectorized form) as an input and returns the trace of the matrix.

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

getTrace.m

function p = getTrace(v)
    len = sqrt(numel(v));
    v = reshape(v,len,len);
    p = sum(diag(v));
end

my_script.m

A = [[7,8,9];[6,1,2];[5,4,3]]
getTrace(A)

output :

If you liked this answer, please give a thumbs up rating. Feel free to comment if you need any clarifications/explanations.Thanks for asking :)

Add a comment
Know the answer?
Add Answer to:
MATLAB function Write an efficient function, f(v) that takes a one-dimensional array of length N^2 for...
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 C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • Write a function that takes as input parameters an array of characters and a length field....

    Write a function that takes as input parameters an array of characters and a length field. The function will create a square matrix from the array by first determining the largest square matrix that will hold as many numbers as possible yet not have any blank spaces. So, for instance, if the array’s length was 22, the largest square matrix would be 4x4. If the array’s length was 37, the square matrix would be 6x6. Then create the square matrix...

  • MATLAB Write a function maxContent(arr) that takes a cell array arr as input and returns the...

    MATLAB Write a function maxContent(arr) that takes a cell array arr as input and returns the longest string and largest number in arr. If there are two character arrays of same length, the function should return one of them. Assume that integers in the cell array are nonnegative.

  • Please use C !!! Write a C function matrixTranspose that takes a two-dimensional array as its...

    Please use C !!! Write a C function matrixTranspose that takes a two-dimensional array as its input argument then transposes its elements and prints the results. Develop the matrixTranspose method with two different methods: 1. Assume the matrix is a square matrix. This will make finding the transpose a simple swapping of the arrays’ elements and it can be done in place. 2. Generalize your function to work with any NxM matrix where N≠M. You will need to properly handle...

  • 1. Write a MATLAB function that takes a matrix, a row number and a scalar as...

    1. Write a MATLAB function that takes a matrix, a row number and a scalar as arguments and multiplies each element of the row of the matrix by the scalar returning the updated matrix. 2. Write a MATLAB function that takes a matrix, two row numbers and a scalar as arguments and returns a matrix with a linear combination of the rows. For example, if the rows passed to the function were i and j and the scalar was m,...

  • Write a method that takes in a two-dimensional array of integers, do some calculation for each...

    Write a method that takes in a two-dimensional array of integers, do some calculation for each row in the two-dimensional array, and returns a one-dimensional array of sums for each row. Your code must work any array dimensions, including jagged arrays. The calculations are: • Sum of each row • Maximum value of each row • Minimum value of each row • Average of each row Here is an example an array passed and returns an array of sums for...

  • Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as...

    Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...

  • Write a C++ function that takes a two-dimensional array and a row index. The function should...

    Write a C++ function that takes a two-dimensional array and a row index. The function should return the multiplication of all the values in the row. Write a main () program to test the function. Student Note: Please solve the question as simple as you can.

  • *** write in matlab *** use for loops largestfactor_for Write a function that takes a positive integer greater than one...

    *** write in matlab *** use for loops largestfactor_for Write a function that takes a positive integer greater than one and returns its largest factor (other than itseir). You must use a for loop to solve this problem. Do not use functions factor), primes(), and divisors() . Do not use while loops Do not use vectorized code. alargestfactor 105) 35 largestfactor_for Write a function that takes a positive integer greater than one and returns its largest factor (other than itseir)....

  • use matlab do clearly 2) Write a MATLAB function that takes a structure array holding information...

    use matlab do clearly 2) Write a MATLAB function that takes a structure array holding information about students (name, age, GPA) and returns this array by ordering the students with respect to their age by ascending ordering (smallest to largest). Use bubble sort algorithm for sorting!!!!!

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
ADVERTISEMENT