Given two input matrices, x and y, check if their inner dimensions match.
Example:
x = [1 2;3 4] y = [5;6]
z = [17;39]
x = [1 2 3;4 5 6] y = [2 5;3 6]
z = "Have you checked the inner dimensions?"
OR
z = "The inner dimensions are 3 and 2. Matrix multiplication is not possible"
-------------------------------------------------------------------------------------------------------------------------------------------------------------
NOTE - An example of combining numbers and strings together is shown below:
x = "The sum of " + 4 + " and " + 3 + " equals " + 7
x =
"The sum of 4 and 3 equals 7"
I solved this question with an if condition and the size of my solution was 39. I found the size of the leading solution to be 14. This is the leading solution: function ans = in_prod(x,y)
"no";
try
x*y;
end
How is this even correct? I tried to paste the same solution instead of mine and the assertion failed. z is not mentioned anywhere in this code. Can someone enlighten me?
If the output x*y is not assigned to any variable, MATLAB assigns it to the variable ans by default. In this solution, z has been replaced with ans in the function definition to return the unassigned output.
I am confused... this ought to work:
function z = in_prod(x,y)
if size(x,2) == size(y,1)
z = x * y
else
z = "The inner dimensions are " + size(x) + " and " + size(y) + ". Matrix multiplication is not possible"
end
All your base are belong to us
428 Solvers
267 Solvers
265 Solvers
211 Solvers
Back to basics - mean of corner elements of a matrix
243 Solvers