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.
Write a C++ program to multiply two matrices a and b and print the result. Use...
Write a C++ program to multiply two matrices a and b and print the result. Use two-dimensional arrays to represent the matrices.
c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
Consider the following snapshot of a system: Allocation Max Available A B C D A B...
Consider the following snapshot of a system: Allocation Max Available A B C D A B C D A B C D P0 0 0 1 2 0 0 1 2 1 5 2 0 P1 1 0 0 0 1 7 5 0 P2 1 3 5 4 2 3 5 6 P3 0 6 3 2 0 6 5 2 P4 0 0 1 4 0 6 5 6 Answer the following questions using the banker’s algorithm: a....
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)
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 .
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.
For the following infix expression, build the corresponding expression tree. 1.1 a*b 1.2 a+b*c 1.3 a+b*c/d-e...
For the following infix expression, build the corresponding expression tree. 1.1 a*b 1.2 a+b*c 1.3 a+b*c/d-e Perform pre-order and post-order traversal of the above binary expression trees. What relationship exists among these scans and prefix and postfix notation for the expression?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT