Question

In the code space provided in Matlab Grader, create a function that: 1. Accepts an input...

In the code space provided in Matlab Grader, create a function that:

1. Accepts an input matrix A as its only input argument. Matrix A is a 3x3 matrix of random integers between 1 and 4

2. Produces and output matrix B as its only output argument. Matrix B will also be a 3x3 matrix.

3. Calculates the elements of matrix B such that: a. the values of B along the diagonal are equal to the respective elements in A b. the values in each element of B not along the diagonal are equal to the sum of the values in the respective row and column of A

Use for loop.

Homework Answers

Answer #1

The function is as follows

function B = matrixAB(A)

for i =1:3
for j = 1:3
if i==j
B(i,j)=A(i,j); % the values of B along the diagonal are equal to the respective elements in A
else
B(i,j) = sum(A(i,:))+sum(A(:,j)); % the non diagonal values in B are equal to the sum of the values in the respective

% row and column of A
end
end
end

end

To get a matrix of random integer values between 1 and 4, we can use

A = randi([1 4],3,3)

where 1 and 4 are the range, 3 and 3 is the number of rows and columns of the matrix required

To check the function, first generate the matrix using the command

A = randi([1 4],3,3)

Now call the function

B = matrixAB(A)

Example Output

>> A = randi([1 4],3,3)

A =

3 3 1
4 3 2
2 1 4

>> B = matrixAB(A)

B =

3 14 14
18 3 16
16 14 4

>>

Know the answer?
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
Similar Questions
Create a function x=backsub(U,y) in matlab that accepts as input arguments a nxn matrix U and...
Create a function x=backsub(U,y) in matlab that accepts as input arguments a nxn matrix U and n-vector y, and returns as output a column vector x consisting of the values of the unknowns x1; x2; : : : ; xn.
using matlab: Q)using nested for loops to generate a 3*3 matrix where the element value is...
using matlab: Q)using nested for loops to generate a 3*3 matrix where the element value is equal to the sum of its row and column number, except for the diagonal elements which are zeros
*** 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 n-by-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 that...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work...
let us create a variable for a row vector a = [1, 4, 1, 3, 2,...
let us create a variable for a row vector a = [1, 4, 1, 3, 2, 5, 0] and calculate the mean value of its elements using the Matlab function ‘mean’ and store this value in variable aMean. Fig. 1 gives the Matlab code to do this. a = [1, 4, 1, 3, 2, 5, 0]; aMean = mean(a); Figure 1: Matlab code – row vector and mean of its elements. Let us now construct a row vector b that...
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the...
Create a function mult_arr which accepts two pointers-to-double as input along with an integer reflecting the length of each array. The function should return void. Using pointer arithmetic to access the array elements, compute the element-wise product of the two arrays in-place (the result is stored in the first array). The function should return void. In main, read in two arrays of 10 values from the user following the I/O format seen in the example run below. On a new...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Part 2: Solve the following problems in MATLAB 1. Fill in the function E = myElim(A,...
Part 2: Solve the following problems in MATLAB 1. Fill in the function E = myElim(A, r_entry, r_pivot, c) to create an m by m elimination matrix ??. Remember that an elimination matrix looks like an identity matrix with one extra entry of ?? in row r_entry and column r_pivot. 2. Fill in the function M = myMult(A, c_pivot) to create an m by m multiplier matrix ??. Remember that a multiplier matrix looks like an identity matrix with the...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits...
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits by following the instructions noted below. The input signal and impulse response generation should be done in the function. The test bench script should be used only to call the function and for signal plotting purposes. No plotting should be done inside the function itself. Name your function L2_C Instructions: Input voltage ,x(t), can be AC or DC. Consider a variable ‘w1’ which can...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT