Help me about matrix ?
1 回表示 (過去 30 日間)
古いコメントを表示
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
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Statistics and Machine Learning Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!