为什么matlab有1个基于索引的 [英] Why does matlab have 1 based indexing

查看:138
本文介绍了为什么matlab有1个基于索引的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾用Java和Python编程,但最近已经开始使用MATLAB来处理大量的东西(特别是计算机视觉算法)。

I used to program in Java and Python earlier, but recently have started using MATLAB for lots of stuff (specifically computer vision algorithms).

然而,MATLAB的索引是数组从1开始而不是0,这是我到目前为止遇到的几乎所有编程语言的标准。

However MATLAB has indexing of arrays beginning from 1 instead of 0, which is the norm in almost every programming languages I have encountered so far.

基于0的索引对我有意义的原因是这样的:
在我看到的每个处理器中,地址索引以0x00000000开头说8位处理器如果我想保存5个数字,它们将存储在地址0x00000000到0x00000004中。因此,在编程语言中从0开始索引是有意义的。

The reason 0-based indexing made sense to me was like this: In every processor I have seen, the address index begins with 0x00000000 say in an 8-bit processor and If I want to save 5 numbers, they would be stored in address 0x00000000 to 0x00000004 . Hence it makes sense to have indexing from 0 in programming languages.

当我在搜索这个问题时,我发现 1索引编程语言列表? http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array)有用。

While I was searching for this question I found List of 1-indexed programming languages? and http://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array) to be useful.

对我来说它不是一个非常方便的表示法,也许是因为我以前用其他语言编写代码。但我仍然不明白为什么MATLAB(现在甚至是Julia)都有一个基于1的数组索引以及它提供的优势。任何人都可以列出这些优势吗?

For me it is not a very convenient notation, maybe because I used to code in other languages before. But I still don't understand why MATLAB (and even Julia now) has a 1-based array indexing and what advantages it provides. Can anyone list out the advantages?

推荐答案

为什么它有基于1的索引?历史原因。 ( Cleve Moler 决定如此)。它可能与基于1的索引是矩阵表示法的约定。

Why does it have 1-based indexing? Historical reasons. (Cleve Moler decided so). It probably has to do with 1-based indexing being the convention for matrix notation.

为什么它也没有实现基于0的索引?
一些精彩的阅读

Why does it not implement 0-based indexing too? Up for some nice light reading?

特别要寻找Cleve Moler(MATLAB的创建者)和Steve Lord(MathWorks工程师)的评论。经过多次交流之后,没有实现基于补充 0的索引的基本推理(至少在2001年)不是出于任何哲学或兼容性原因,尽管它起初是一个向后兼容性论证,但是因为它需要花费太多精力来更新MATLAB内置函数:

In particular, look for comments from Cleve Moler (the creator of MATLAB) and Steve Lord (MathWorks Engineer). After much exchange, the basic reasoning (at least in 2001) for not implementing a supplementary 0-based indexing was not for any philosophical or compatibility reasons, although it started as a backward compatibility argument, but because it would require just too darn much effort to update MATLAB built-in functions:


我同意可以添加从零开始的索引到MATLAB,
使用新表示法或重载所有
现有索引表示法的对象。它可以在
意义上向后兼容,不知道或使用新东西的旧代码仍然可以
工作。

I agree that it would be possible to add zero-based indexing to MATLAB, using either new notation or an object that overloaded all of our existing indexing notation. It could be backwards compatible in the sense that old code that didn't know about or use the new stuff would still work.

但是这里是关键点:我们的现有代码中没有一个可以使用
新对象。你不能绘制它;你打不开它;你
肯定不能用它做任何矩阵操作。是的,随着时间的推移,
所有内容都可以重写以处理扩展,但是
是我们想要避免的。它就像AM和FM收音机 - 两个
系统生活在同一个盒子里,但有独立的独立
底层技术。

But here is the key point: none of our existing code would work with the new object. You couldn't plot it; you couldn't print it; you certainly couldn't do any matrix operations with it. Yes, over time, everything could be rewritten to handle the extension, but that is what we want to avoid. It would be like AM and FM radio -- two systems living in the same box, but with separate and independent underlying technology.

- - Cleve Moler

moler@mathworks.com

-- Cleve Moler
moler@mathworks.com

翻译:他们不想写所有新剧情,打印等惯例。

Translation: They don't want to write all new plot, print, etc. routines.

这篇关于为什么matlab有1个基于索引的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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