Colon operands must be real scalars
478 views (last 30 days)
Show older comments
Alessandro
on 12 Sep 2024
Commented: Alessandro
on 12 Sep 2024
I tested the following code:
x = rand(1,3);
x([1,2]:[1,2])
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);
3 Comments
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
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.
Accepted Answer
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
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]
whatItDoes = 1:1
alternate1 = [1:1 2:2] % Each element of a is "combined" with the corresponding element of b
alternate2 = [1 2:1 2] % 2:1 is empty
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]
actual3 = [1 2]:[3 999]
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)
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:height(A)
v = 1:size(A, 2)
v = 1:width(A)
v = 1:numel(A)
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)
indices = a:100
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.
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!