Help me about matrix ?

1 visualizzazione (ultimi 30 giorni)
Nguyen Trong Nhan
Nguyen Trong Nhan il 21 Dic 2013
Risposto: Image Analyst il 21 Dic 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.

Risposta accettata

Image Analyst
Image Analyst il 21 Dic 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

Più risposte (0)

Categorie

Scopri di più su Statistics and Machine Learning Toolbox in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by