# Meaning of the if statement

2 vues (au cours des 30 derniers jours)
Muhtasim Haque Nahian le 19 Avr 2021
function z = in_prod(x,y)
if size(x,2)==size(y,1)
z=x*y
else
z = "The inner dimensions are " + size(x,2) + " and " + size(y,1) + ". Matrix multiplication is not possible";
end
• Can anyone explain me the meaning of this if statement please?
• More specifically, why size(x,2) and size(y,1)? Why not size(x,any other value)?
TIA!
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Stephen23 le 19 Avr 2021
"More specifically, why size(x,2) and size(y,1)?"
Because matrix multiplication is only defined when those dimensions are equal:
"Why not size(x,any other value)?"
Because matrix multiplication is only defined for 2D matrices.

Connectez-vous pour commenter.

### Réponses (1)

David Fletcher le 19 Avr 2021
Modifié(e) : David Fletcher le 19 Avr 2021
The if statement is checking that the number of columns in x (the number two in the argument of size is specifying that the function returns the number of columns) matches the number of rows in y (the number one in the argument of size is specifying that the function returns the number of rows). This ensures that matrix multiplication can be applied to x and y. If the inner dimensions do not match, the matrices cannot be multiplied.
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Muhtasim Haque Nahian le 9 Mai 2021
Thanks for the explanation! It helped

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by