Help me about matrix ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Nguyen Trong Nhan
el 21 de Dic. de 2013
Respondida: Image Analyst
el 21 de Dic. de 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.
Respuesta aceptada
Image Analyst
el 21 de Dic. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!