Colon operands must be real scalars

482 views (last 30 days)
I tested the following code:
x = rand(1,3);
x([1,2]:[1,2])
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
ans = 0.1995
In R2024a it gives a warning but it is ok in R2023b! Some existing code that I use relies on this feature. Any reason why it will become an error in the future?
For a better example of how using non-scalar colon operands may be useful:
clear,clc
Params.agejshifter.College=21;
Params.J.College=100-Params.agejshifter.College;
Params.agej.College=1:1:Params.J.College;
Params.J.College=100-Params.agejshifter.College; % Age ends at 100
dj_temp=interp1([0,30,60,65,70,100],[0.00587,0.00116,0.01086,0.01753,0.02785,0.39134],0:1:100,'linear');
Params.sj.College=1-dj_temp((Params.agej.College+Params.agejshifter.College):100);
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
  3 Comments
Image Analyst
Image Analyst on 12 Sep 2024
Your example had a 3 element vector
x = rand(1,3);
Which element(s) do you think you want to refer to when you do x([1,2]:[1,2]) ? The first one, second one, third one, or the first and second one (like x(1:2))???
Stephen23
Stephen23 on 12 Sep 2024
"Any reason why it will become an error in the future?"
Because it causes plenty of bugs, misleads users, and is trivially replaced with some indexing.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Sep 2024
The colon operator has always been defined as using the first element of each operand.
The change is that soon using non-scalar operands will be an error instead of silently permitted.
Params.sj.College=1-dj_temp((Params.agej.College+Params.agejshifter.College):100);
That code does not create some kind of ragged array indexing from each Params.agej.College+Params.agejshifter.College pair to 100. That code is equivalent to
Params.sj.College=1-dj_temp((Params.agej.College(1)+Params.agejshifter.College(1)):100);
It has always been equivalent to that.

More Answers (1)

Steven Lord
Steven Lord on 12 Sep 2024
For the expression you gave there are a few possible "reasonable" solutions for what it returns. [But it may not do what you think.]
actual = [1 2]:[1 2]
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
actual = 1
whatItDoes = 1:1
whatItDoes = 1
alternate1 = [1:1 2:2] % Each element of a is "combined" with the corresponding element of b
alternate1 = 1×2
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
alternate2 = [1 2:1 2] % 2:1 is empty
alternate2 = 1×2
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But there are other cases where it's difficult to give a reasonable alternative (other than the "just use the first element" approach it's been using for a while.)
actual2 = [1 2]:[1 2 3]
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
actual2 = 1
actual3 = [1 2]:[3 999]
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
actual3 = 1×3
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In addition, upon reviewing usage of : with non-scalar inputs in MathWorks code we've found that often that usage is a bug!
A = ones(3, 4);
v = 1:size(A)
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
v = 1×3
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Did you expect v to be 1:3 or 1:4 or did you expect it somehow to be a combination of those two vectors? If you expected it to be 1:height(A) then you got what you expected; if you expected 1:width(A) or 1:numel(A) you weren't getting what you wanted!
v = 1:size(A, 1)
v = 1×3
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v = 1:height(A)
v = 1×3
1 2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v = 1:size(A, 2)
v = 1×4
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v = 1:width(A)
v = 1×4
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v = 1:numel(A)
v = 1×12
1 2 3 4 5 6 7 8 9 10 11 12
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Let's look at your example.
Params.agejshifter.College=21;
Params.J.College=100-Params.agejshifter.College;
Params.agej.College=1:1:Params.J.College;
Params.J.College=100-Params.agejshifter.College; % Age ends at 100
dj_temp=interp1([0,30,60,65,70,100],[0.00587,0.00116,0.01086,0.01753,0.02785,0.39134],0:1:100,'linear');
a = (Params.agej.College+Params.agejshifter.College)
a = 1×79
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
indices = a:100
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
indices = 1×79
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
What exactly were you hoping indices would be when you used it as an index into dj_temp? Did you somehow hope that it would be a collection of vectors, first 22:100 then 23:100 then 24:100 etc.? It is not. So if you thought it was behaving that way, your code has a bug.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!