Help me about matrix ?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nguyen Trong Nhan
le 21 Déc 2013
Réponse apportée : Image Analyst
le 21 Déc 2013
help me write a code for this program type input 2 matrix A and B. Check whether A can multiply B ? If yes, calculate each of elements of the product matrix and pay out the product matrix. thanks very much.
Réponse acceptée
Image Analyst
le 21 Déc 2013
Is this homework? Sounds like it. As you know, to do a matrix multiplication you need to have the number of columns of the first matrix equal the number of rows of the first matrix. You can use the size() function and an if statement to do this check. This is probably what your instructor meant. If you want to do an element by element multiplication you need to have both the number of rows match, and the number of columns match. But it's the same process - use size() and then test with "if". I'll leave the simple details up to you since I think it's your homework.
A way to do it that is probably not what your instructor is thinking of is to use try catch and let MATLAB automatically decide if they can be multiplied or not:
try
AB = A * B; % or use A .* B for element-by-element multiplication.
catch ME
% Get here if they cannot multiply. Give user the error message.
errorMessage = sprintf('Matrices cannot multiply.\nError in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg((errorMessage));
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Statistics and Machine Learning Toolbox 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!