通常使用end关键字作为在Matlab中访问或扩展数组的快捷方式,如同
>> x = [1,2,3];
>> x(1:end-1)
ans =
1 2
>> x(end+1) = 4
x =
1 2 3 4
但是,我惊讶地发现以下内容也起作用
>> x(1:min(5, end))
ans =
1 2 3 4
我认为这可能是一个特殊的形式,如:可以特别的索引操作,所以我创建了一个类来检测这个
classdef IndexDisplayer
methods
function subsref(self, s)
disp(s);
end
end
end
您可以在以下示例中看到如何:特殊套装
>> a = IndexDisplayer;
>> a(1:3)
type: '()'
subs: {[1 2 3]}
>> a(:)
type: '()'
subs: {':'}
但是,当我索引结束时,我看到
>> a(end)
type: '()'
subs: {[1]}
这里的最后被替换为1. 1来自哪里?我的第一个猜测是,索引表达式x(end)中的任何结尾将被替换为对length(x)的调用,所以我尝试覆盖长度
classdef IndexDisplayer
methods
function subsref(self, s)
disp(s);
end
function len = length(self)
len = 10;
end
end
end
但是,这给了
>> a = IndexDisplayer;
>> length(a)
ans =
10
>> a(end)
type: '()'
subs: {[1]}
使理论走出窗外。任何人都可以解释结束语义吗?