Problem 44950. Calculate Inner Product
- If they match, create an output variable z which contains the product of x and y
- Otherwise, z should contain a custom string message
Solution Stats
Problem Comments
-
11 Comments
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
This is dot product, not inner product, as the title suggests. They are not exactly the same.
I personally suggest substituting the title with 'dot product', as the description states.
What is wrong with this code? It's working on the downloaded version.
[xrow,xcol]=size(x);
[yrow,ycol]=size(y);
if xcol==yrow
z = x*y
else
z='Have you checked the inner dimensions?'
end
please help me,what is wrong with this code?fail the third test...
function z = in_prod(x,y)
if size(x,2)==size(y,1);
z=x*y
else
z=''Have you checked the inner dimensions'
end
end
Thank you
DO NOT use disp when printing the string for z, just use z = "string message". Otherwise even if your code is correct it will not complete the problem.
Why adding z=[]; after z="Have you....?" is failing in Test case?
This is grossly misleading for aspiring linear algebra students. The product calculated in this example is called a "matrix multiplication product", not a "inner product". The "inner product" is a generalization of the dot product.
Solution Comments
Show commentsProblem Recent Solvers7195
Suggested Problems
-
It dseon't mettar waht oedrr the lrettes in a wrod are.
1809 Solvers
-
Project Euler: Problem 9, Pythagorean numbers
1150 Solvers
-
790 Solvers
-
5142 Solvers
-
Find the max element of the array
1525 Solvers
More from this Author13
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!