Question

In: Advanced Math

The command C = max(A,B) compares corresponding values in the A and B matrices assuming they...

The command C = max(A,B) compares corresponding values in the A and B matrices assuming they are the same size. The problem is that MATLAB’s native version does not tell you from which matrix it found the largest value. Write a new function (in MATLAB) [C,from_which] = max_new(A,B) that also output a matrix, the same size as the inputs, with either 1’s or 2’s depending on from which matrix the larger value was found. Include a live script that tests your solution.

Solutions

Expert Solution

MATLAB Script:

close all
clear
clc

A = [1 2; 3 4]
B = [3 1; 2 5]
[C, from_which] = max_new(A, B)

function [C, from_which] = max_new(A, B)
if size(A) == size(B)
from_which = zeros(size(A));
C = zeros(size(A));
for i = 1:size(A,1)
for j = 1:size(A,2)
if A(i,j) > B(i,j)
from_which(i,j) = 1;
C(i,j) = A(i,j);
else
from_which(i,j) = 2;
C(i,j) = B(i,j);
end
end
end
else
error('Invalid operation. Matrices are of different sizes.')
end
end

Live Script Screenshot:


Related Solutions

Complete the following program so that C = A*B where A and B are the matrices...
Complete the following program so that C = A*B where A and B are the matrices defined below. N=4; M=3; A=rand(M,N); B=rand(N,M); C = ? for m = 1: M    for n = 1 : ? for p = 1 : ? C (m,n) = ?       end    end end You may upload a script
(a) The n × n matrices A, B, C, and X satisfy the equation AX(B +...
(a) The n × n matrices A, B, C, and X satisfy the equation AX(B + CX) ?1 = C Write an expression for the matrix X in terms of A, B, and C. You may assume invertibility of any matrix when necessary. (b) Suppose D is a 3 × 5 matrix, E is a 5 × c matrix, and F is a 4 × d matrix. Find the values of c and d for which the statement “det(DEF) =...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
generate the following matrices with given rank and verify with the rank command. Include the Matlab...
generate the following matrices with given rank and verify with the rank command. Include the Matlab sessions in your report as indicated. A) is 8x8 with rank 3. B) is 6x9 with rank 4. C) is 10x7 with rank 5 .
Consider A, B and C, all nxn matrices. Show that: 1) det(A)=det(A^T) 2) if C was...
Consider A, B and C, all nxn matrices. Show that: 1) det(A)=det(A^T) 2) if C was obtained from A by changing the i-th row (column) with the j-th row (column). Show that det(C)=-det(A) 3) det(AB)=det(A)det(B) 4) Let C be a matrix obtained from A by multiplying a row by c ∈ F. Show that det(B)=c · det(A)
The values of y and their corresponding values of y are shown in the table below...
The values of y and their corresponding values of y are shown in the table below x 2 3 4 4 6 y 2 3 5 4 6 A) Calculate the coefficient of correlation; B) Calculate the coefficient of determination; C) Obtain the regression coefficients and write the regression expression; D) Provide your prediction of the dependent variable if the value of the independent variable is 4.
Given the nxn matrices A,B,C of real numbers, which satisfy the Condition: A+B+λΑΒ=0 Β+C+λBC=0 A+C+λCA=0 for...
Given the nxn matrices A,B,C of real numbers, which satisfy the Condition: A+B+λΑΒ=0 Β+C+λBC=0 A+C+λCA=0 for some λ≠0 ∈ R (α) Prove that I+λΑ,Ι+λΒ,Ι+λC are invertible and AB=BC=CA. (b) Prove that A=B=C
Calculate the Y values corresponding to the X values given below. Find the critical values for...
Calculate the Y values corresponding to the X values given below. Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d¬2y/dx2 = 0. Be sure to indicate the sign (+ or -) of dy/dx and of d2y/dx2 tabled values. Using the first and second derivative tests with the information you have calculated, determine which X value(s) represent...
Calculate the Y values corresponding to the X values given below. Find the critical values for...
Calculate the Y values corresponding to the X values given below. Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d­2y/dx2 = 0.    Be sure to find the sign (+ or -) of dy/dx and of d2y/dx2 at all X values. Reference Lesson 13 and the text Appendix A (pp 694 – 698), as needed. Using the...
Calculate the Y values corresponding to the X values given below. Find the critical values for...
Calculate the Y values corresponding to the X values given below. Find the critical values for X for the given polynomial by finding the X values among those given where the first derivative, dy/dx = 0 and/or X values where the second derivative, d­2y/dx2 = 0. Be sure to indicate the sign (+ or -) of dy/dx and of d2y/dx2 tabled values. Reference Power Point Lesson 13 as needed. Using the first and second derivative tests with the information you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT