Help me about matrix ?

조회 수: 1 (최근 30일)
Nguyen Trong Nhan
Nguyen Trong Nhan 2013년 12월 21일
답변: Image Analyst 2013년 12월 21일
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.

채택된 답변

Image Analyst
Image Analyst 2013년 12월 21일
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개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by