Making a Matrix calculator

I would like to make a calculator that would ask the user for the number of matrices involved, and then add, subtract, divide, or multiply said matrices. After some digging around, I think I could use while loops, and some switch/case loops to pull this off. I am wondering if this would be the best way to approach this, or if there were some other loops I might not have considered that could help me out. Any advice or suggestions would be appreciated.

9 件のコメント

dpb
dpb 2025 年 3 月 26 日
Whatever you do, do NOT create the input arrays as sequentially-numbered variables like M1, M2, ..., etc.
Use structures or cell arrays or other higher abstraction data that can be manipulated without having to resort to evil eval
It's unfortunate that Mathworks didn't let plus, + have a variable number of arguments; then one could just call it with a list, but it and its brethren are limited to two arguments only.
Stephen23
Stephen23 2025 年 3 月 26 日

I don‘t see why you would need any WHILE loops for this: a cell array and FOR loops would likely be sufficient.

Ray
Ray 2025 年 3 月 26 日
編集済み: Ray 2025 年 3 月 26 日
@dpb Thank you for this advice. I am planning to use a cell array, something akin to matrices = cell(1, numMatrices);
I plan on initializing a zero matrix, that can then be populated with matrix elements from the user
Ray
Ray 2025 年 3 月 26 日
@Stephen23 I plan on using the while loops for if the user inputs something invalid. I want to give the user a chance to correct their mistake without re-running the code. Like if the user put -1 for the number of rows/columns for example.
dpb
dpb 2025 年 3 月 26 日
@Ray, a nicety would be to allow a user a list of indices and values -- like eye(3) is only three nonzero elements whereas the full array would require entering all nine.
Stephen23
Stephen23 2025 年 3 月 26 日
"I plan on using the while loops for if the user inputs something invalid"
Ray
Ray 2025 年 3 月 28 日
@Stephen23 Thank you for that link. I would like to try to add a UI later.
Ray
Ray 2025 年 3 月 28 日
@dpb as tedious as it would be, I would like the user to input all the elments.
Ray
Ray 2025 年 3 月 28 日
Thank you both, I have a fundamentally working program! I can post it if either of you are interested in critiquing me.

サインインしてコメントする。

回答 (3 件)

Prasanna
Prasanna 2025 年 4 月 3 日

0 投票

For Matrix Calculator, try this IF ELSE Case.
Take this example. It might be helpful for you.
a = input('Enter First Matrix: ');
b = input('Enter Second Matrix: ');
out = input('Operation to do: '); % ADD = 1/SUBTRACT = 2/MULTIPLY = 3/DIVIDE = 4
if out == 1
output_add = a+b
elseif out == 2
output_subtract = a-b
elseif out == 3
output_product = a*b
elseif out == 4
output_divide = a/b
else
fprintf('No proper input')
end
Umeshraja
Umeshraja 2025 年 8 月 18 日
編集済み: Umeshraja 2025 年 8 月 18 日

0 投票

Hello @Ray,
If you’re just starting out with MATLAB and want a solid foundation, I recommend first completing the MATLAB Onramp course. It offers a great introduction to MATLAB basics and will help you feel more comfortable working with the environment.
To help build your matrix calculator, here are some useful resources to guide you:
Hope this helps!
ali
ali 2025 年 11 月 29 日

0 投票

can we change it a little bit. you can totally build your matrix calculator without drowning in while-loops and switch-cases. The big thing is: don’t make variables like M1, M2, M3…Just throw all your matrices into a cell array and loop through them — way cleaner and way less headache.
Use a while loop only when the user screws up the input, like entering -1 for rows. That’s fine. But for the actual matrix operations, a simple for-loop is all you need.
So the flow is basically: ask how many matrices they want, store them in a cell array, then run whatever operation they chose across that list. MATLAB doesn’t let you do multi-argument A + B + C + D, so you just start with the first matrix and keep stacking the operations in a loop.
Here:
result = matrices{1};
for i = 2:numMatrices
result = result + matrices{i}; % or -, *, /
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

Ray
2025 年 3 月 26 日

回答済み:

ali
2025 年 11 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by