More unexpected behavior multiplying an array with a double

4 views (last 30 days)
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
Kristoffer Walker
Kristoffer Walker on 27 Apr 2025
Thanks Stephen. I guess that means either my memory is failing, or it is so non-intuitive that my experience with Matlab is not sufficient to enable me to figure it out again easily. Happy Sunday, and thanks again for your kind reply.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 27 Apr 2025
Using the simpler example that @Stephen23, posted
S(1).x = pi;
S(2).x = sqrt(2);
S.x
ans = 3.1416
ans = 1.4142
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
ans = 1×2
1.0e+03 * 3.1416 1.4142
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 Comment
Kristoffer Walker
Kristoffer Walker on 27 Apr 2025
Thank you for the quick reply and explaining that the format is a comma separated list. I tried using the "class" function earlier to interrogate that, but it did not report that format. I was able to get your approach to work by using the vertcat() function first:
K>> [app.data.stat.gCorTide]*1e3
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
K>> vertcat(app.data.stat.gCorTide)*1e3
ans =
-36.6000
-36.6000
-36.6000

Sign in to comment.

More Answers (1)

Stephen23
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 = 1x2 struct array with fields:
x
S.x*1e3
Error using *
Too many input arguments.
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
Kristoffer Walker
Kristoffer Walker on 27 Apr 2025
Stephen, thanks for the quick reply! I followed the link above, but 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. Matlab excels in having ways to dereference and cast elements in different formats into a matrix for these types of operations. Do you know of any way to do this on one line instead of the two lines that I currently have to use?
Stephen23
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.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!