How to write a product of two numbers 6 and 7 in Matlab code to get 42

52 views (last 30 days)
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.

Answers (3)

Walter Roberson
Walter Roberson on 8 Oct 2025 at 15:47
times(6,7)
ans = 42
  4 Comments
Steven Lord
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
y = 720
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
x = 10×11
1 4 3 1 4 2 1 2 5 4 3 3 5 5 5 1 5 1 2 5 4 1 5 2 5 2 2 3 3 3 5 1 4 3 4 1 5 1 5 2 3 4 3 1 4 1 1 3 5 2 4 3 4 1 5 2 4 3 2 3 5 2 5 1 2 1 1 4 2 2 3 2 3 1 3 3 2 1 2 5 3 4 2 3 2 4 3 5 4 1 3 1 3 3 2 5 1 5 1 3 4 1 2 5 3 3 1 3 1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = prod(x, "all")
y = 1.3101e+43
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)
ans = logical
1
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
x1 = 42
x2 = 6.*7
x2 = 42
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])
x =
x(:,:,1) = 1 2 3 4 x(:,:,2) = 5 6 7 8
y = prod(x, 3) % Take the product in dimension 3
y = 2×2
5 12 21 32
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
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.

Sign in to comment.


DGM
DGM on 8 Oct 2025 at 16:41
Moved: Image Analyst on 14 Oct 2025 at 1:04

Image Analyst
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
p = 42
For other kinds of multiplication, see the list below:
help multiplication
--- multiplication not found. Showing related topics. --- conv - Convolution and polynomial multiplication pagemtimes - Page-wise matrix multiplication double.mtimes - Matrix multiplication .* - Multiplication jc_0610: Operator order for multiplication and division block - jc_0611: Input sign for multiplication and division blocks - Use floating-point multiplication to handle net slope corrections - Net slope correction computation for floating-point to fixed-point casts gmultiply - Generalized multiplication nnet.cnn.layer.MultiplicationLayer - Multiplication layer conv - Convolution and polynomial multiplication of fi objects pow2 - Efficient fixed-point multiplication by 2^{K} quaternion.mtimes - Quaternion multiplication .* - Element-wise quaternion multiplication linearArray.patternMultiply - Radiation pattern of array using pattern multiplication quaternion.mtimes - Quaternion multiplication .* - Element-wise quaternion multiplication .* - Element-by-element multiplication of fi objects quaternion.mtimes - Quaternion multiplication .* - Element-wise quaternion multiplication se2.mtimes - Transformation or rotation multiplication Search the documentation for multiplication docsearch multiplication

Community Treasure Hunt

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

Start Hunting!