More unexpected behavior multiplying an array with a double
4 views (last 30 days)
Show older comments
Hello,
I am getting unexpected results when I muliply an array by a double. Here is the code:
switch app.CorrDropDown.Value
case "Load"
y = app.data.stat.gCorLoad*1e3;
end
The contents of app.data.stat.gCorLoad are this:
K>> app.data.stat.gCorLoad
ans =
-0.0044
-0.0044
-0.0043
-0.0043
-0.0043
-0.0043
The class is:
K>> class(app.data.stat(1).gCorLoad)
ans =
'double'
Yet, when the above code executes, it complains on the simple array multiplication:
K>> y = app.data.stat.gCorLoad*1e3;
Error using *
Too many input arguments
The only way for me to fix this is to assign gCorLoad to y first. Then on the next line, do a y = y * 1e3. This is frustrating. I do not remember having problems like this in the past and wonder if this is related to a new way to handling structs. Matlab should be smart enough to handle this basic array multiplication.
Also, I tried to submit this form in Edge, and it fails to submit. It quietly ignores the submitter when the submitter presses the "Post Question" button. I had to open Chrome and copy-n-paste all these form elements into it in order to post this.
2 Comments
Accepted Answer
the cyclist
on 27 Apr 2025
S(1).x = pi;
S(2).x = sqrt(2);
S.x
Note that S.x is not a double array. It is a comma-separated list. So, MATLAB is seeing too many inputs in the next operation.
One simple way to deal with this is combined the comma-separated list into an array:
[S.x]*1e3
More Answers (1)
Stephen23
on 27 Apr 2025
Edited: Stephen23
on 27 Apr 2025
"I am getting unexpected results when I muliply an array by a double"
It is not unexpected, because your data are not what you think they are.
Your example is very simple to replicate given a non-scalar structure:
S(1).x = pi;
S(2).x = sqrt(2)
S.x*1e3
The TIMES operator is only defined for exactly one matrix of the left of the operator and exactly one array on the right of the operator, thus your useage (with a non-scalar structure) is not expected to work:
"I do not remember having problems like this in the past and wonder if this is related to a new way to handling structs."
Generating comma-separated lists from non-scalar structures has existed since at least 2012:
"Matlab should be smart enough to handle this basic array multiplication."
MATLAB is certainly smart enough to handle basic matrix multiplication, when a user tells it to do basic matrix multiplication. However when a users tells it to perform one single matrix multiplication on three (or more) matrices at once then MATLAB quite rightly throws an error. As far as I am aware, one single matrix multiplication on three or more arrays is not defined mathematically.
The only problem seems to be that a user did not know or check what kind of data they have.
2 Comments
Stephen23
on 27 Apr 2025
"...I could not understand how to use that information to dereference the floating points into a single matrix on the left side so that the times operation would be successful"
Joining separate arrays into one array is called concatenation. MATLAB has several concatenation operators/functions which you can use, try one of these (which one (if any) depends on your data and the expected output):
[app.data.stat.gCorLoad]
horzcat(app.data.stat.gCorLoad)
vertcat(app.data.stat.gCorLoad)
cat(N,app.data.stat.gCorLoad) % for some N
or any other appropriate function which accepts multiple input arrays. That is the point of comma-separated lists.
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!