Colon operands must be real scalars
조회 수: 487 (최근 30일)
이전 댓글 표시
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
Image Analyst
2024년 9월 12일
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
2024년 9월 12일
"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.
채택된 답변
Walter Roberson
2024년 9월 12일
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.
추가 답변 (1개)
Steven Lord
2024년 9월 12일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!