How to write a product of two numbers 6 and 7 in Matlab code to get 42
52 views (last 30 days)
Show older comments
I have forgotten how to write a numbers product. What is the multiplication symbol in Matlab?
If I want to get 42, how do I write '6 "times what symbol" 7' on a Matlab command line? A simple dot . between them does not work and neither does an x.
So does anyone ever multiply simple numbers such as '1,087 $' "times" 1.08 percent' for finding out credit card costs ? In Matlab code.
Thanks
I have looked through the Documentation and there is no such simple question or answer.
0 Comments
Answers (3)
Walter Roberson
on 8 Oct 2025 at 15:47
times(6,7)
4 Comments
Steven Lord
on 9 Oct 2025 at 17:46
Nice answer. So multiplying two numbers needs the 'times' command in front of the two numbers. How to multiply 6 numbers tom get their product?
Mutiplying numbers requires calling the times function or using the * or .* operators. For the question you asked here, use the prod function.
x = [1 2 3 4 5 6];
y = prod(x) % 6! or factorial(6) for this specific x vector
How about multiplying all number entries of a 10 by 11 matrix together? That would take a doo0000zens of lines of code. BAD ! Or am I too dumb to see a better way ... ?
That's prod as well, as long as you tell it to take the product over all the dimensions.
x = randi(5, 10, 11) % Sample matrix with random integers between 1 and 5
y = prod(x, "all")
But for matrix multiplications, then the * could create the product of two 1 by 1 matrices.
It does.
And it would take a couple of 'reshape' commands ... to switch from the umber realm to matrix realm and back. ... ? ? ?? ...
I assume "umber" is a typo for "number". I think you're drawing a distinction between scalar values (plain numbers) and matrices. MATLAB doesn't make that distinction; a scalar value is a matrix.
ismatrix(42)
How about creating 1 by 1 matrices for each of the numbers and then multiplying these number matrices via * ?
For scalars, matrix multiplication (with the * operator) and element-wise multiplication (with the .* operator) are the same.
x1 = 6*7
x2 = 6.*7
I did not realize, Matlab was running so confusingly. Why I wonder, Cleve and the computation experts at Mathworks ... , why, oh "WHY" ?
What would the number of code statement be to multiply 20 matrices of compatible sizes together? How about 200 such matrices? Coding for a week, it seems and no results until all lines of code are flawlessly written down on a page + of commnands.
If you're creating 20 or 200 individual matrices, that's a sign that you're likely creating variables with numbered names (x1, x2, x3, ...) Can you do that? Yes. Should you do that? The general consensus is no.
If you wanted to multiply those individual matrices element-wise, I would store them in a 3-dimensional array and then use prod with a dimension input argument. In the example below, the answer ought to be [1*5, 2*6; 3*7, 4*8] or [5, 12; 21, 32]
x = cat(3, [1 2; 3 4], [5 6; 7 8])
y = prod(x, 3) % Take the product in dimension 3
And the Matlab Documentations do not mention any such 'mundane' task of simple multiplications at all.
Searching the MATLAB documentation for "multiply" gives an AI answer that references the two multiplication operators. I note that the pages in the documentation for MATLAB don't show up until page 5 of the raw search results; if you post what you searched for I could pass that along to the documentation staff to see if they can make the MATLAB results show up earlier.
If you opened the documentation itself, the second page of the Get Started with MATLAB documentation talks about matrix and element-wise multiplication. The Language Fundamentals category also includes a sub-category Operators and Elementary Operations that talks about Arithmetic Operations.
So the documentation does discuss "simple multiplications".
Since you seem like you're a new(ish) user of MATLAB, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
Walter Roberson
on 9 Oct 2025 at 22:04
Edited: Walter Roberson
on 14 Oct 2025 at 4:36
times(6,7) is the formal internal operation for 6 .* 7 -- that is, 6 .* 7 is internally rewritten into times(6,7). (This has some implications for overloading operators: you can overload the .* operator by overloading the times function.)
mtimes(6,7) is the formal internal operation for 6 * 7 .
The times operator is element-by-element multiplication.
The mtimes operator is algebraic matrix multiplication.
If you happen to * between a matrix and a scalar, then it "devolves" into an internal .* operation.
DGM
on 8 Oct 2025 at 16:41
Moved: Image Analyst
on 14 Oct 2025 at 1:04
You mean like
p = 6*7
p = 1087*1.08/100
See also:
https://www.mathworks.com/help/matlab/arithmetic-operators.html (unroll the sections to see the hidden links)
0 Comments
Image Analyst
on 14 Oct 2025 at 1:12
Did you try getting help on multiplication? For simple multiplication it's simply * like it is in every other language I've used
p = 6 * 7
For other kinds of multiplication, see the list below:
help multiplication
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!