Matlab中'end'的语义是什么? [英] What are the semantics of 'end' in Matlab?

查看:1060
本文介绍了Matlab中'end'的语义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常使用 end 关键字作为在Matlab中访问或扩展数组的快捷方式,如

It's common to use the end keyword as a shortcut for accessing or extending an array in Matlab, as in

>> x = [1,2,3];
>> x(1:end-1)
ans =
    1   2
>> x(end+1) = 4
x =
    1   2   3   4

但是,我很惊讶地发现以下内容也有效

However, I was surprised to find that the following also works

>> x(1:min(5, end))
ans =
    1   2   3   4

我认为 end 可能是一个特殊形式,如,可以是特殊形式的索引操作,所以我创建了一个类来检测这个

I thought that end might be a special form, like :, that can be special-cased in indexing operations, so I created a class to detect this

classdef IndexDisplayer
  methods
    function subsref(self, s)
      disp(s);
    end
  end
end

你可以看到在以下示例中是特殊的

You can see how : is special cased in the following example

>> a = IndexDisplayer;
>> a(1:3)
    type: '()'
    subs: {[1 2 3]}
>> a(:)
    type: '()'
    subs: {':'}

但是,当我使用结束进行索引时,我只看到

However, when I index with end I just see

>> a(end)
    type: '()'
    subs: {[1]}

此处 end 替换为 1 1 来自哪里?我的第一个猜测是索引表达式 x(结束)中的任何结束将替换为对<$的调用c $ c> length(x)所以我尝试覆盖长度以及

Here the end is replaced with a 1. Where does that 1 come from? My first guess was that any end inside an indexing expression x(end) would be replaced with a call to length(x) so I tried overriding length as well

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]}

所以理论不在窗外。谁能解释 end 的语义?

so that theory is out the window. Can anyone explain the semantics of end?

推荐答案

首先,我认为你的语法 x(1:min(5,end))是一种错误,或者至少是一种意想不到的功能。当我在MathWorks时,我记得有人指出这一点,并且相当多的开发人员不得不花费一些时间来弄清楚发生了什么。我不确定他们是否真的同意这是否是一个问题。

Firstly, I think it's kind of a bug, or at least an unexpected feature, that your syntax x(1:min(5, end)) works at all. When I was at MathWorks, I remember someone pointing this out, and quite a few of the developers had to spend a while figuring out what was going on. I'm not sure if they ever really agreed whether it was a problem or not.

解释 end的(预期)语义 end 实现为函数 ind = end(obj,k,n) k 是包含 end 的表达式的索引, n 是表达式中索引的总数。

To explain the (intended) semantics of end: end is implemented as a function ind = end(obj, k, n). k is the index of the expression containing end, and n is the total number of indices in the expression.

因此,例如,当您调用 a(1,结束,1) k 为2,因为 end 在参数2中,并且 n 是3,因为有3个参数。

So, for example, when you call a(1,end,1), k is 2, as the end is in argument 2, and n is 3 as there are 3 arguments.

ind 作为可以在表达式中替换 end 的索引。

ind is returned as the index that can replace end in the expression.

您可以重载结束用于您自己的类(与您重载冒号大小,<$ c相同) $ c> subsref 等)。

You can overload end for your own classes (in the same way as you can overload colon, size, subsref etc).

扩展你的例子:

classdef IndexDisplayer
  methods
    function ind = end(self,k,n)
        disp(k)
        disp(n)
        ind = builtin('end', self, k, n);
    end
  end
end

>> a = IndexDisplayer;
>> a(1,end,1)
 2
 3

参见这里了解更多信息。

这篇关于Matlab中'end'的语义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆