MATLAB中冒号运算的组合 [英] Combination of colon-operations in MATLAB

查看:250
本文介绍了MATLAB中冒号运算的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于冒号运算符和MATLAB中向量展开的问题.我的问题是了解下面的代码行是如何扩展的,以便能够将其用于其他序列. MATLAB代码行是:

I have a question concerning the colon operator and expansion of vectors in MATLAB. My problem is to understand how the following line of code expands, to be able to use it for other sequences. The line of MATLAB code is:

a(1:2:5) = 1:-4:-7 

请注意,a在扩展之前未定义.这将返回向量

Note that a is not defined before the expansion. This returns the vector

a = 1 0 3 0 -7

我知道冒号运算符如何与{start}:{step}:{stop}一起使用,我的问题是理解a(1:2:5)1:-4:-7的组合如何以及为什么返回在位置25处具有零的五个元素的向量?

I know how the colon operator works with {start}:{step}:{stop}, my problem is to understand how and why the combination of a(1:2:5)and 1:-4:-7 returns a vector of five elements with zeros in position 2 and 5?

推荐答案

每当Matlab检测到您将元素确定为矩阵/数组的当前边界 outside 时,它将自动填充缺少的元素零的元素:

Whenever Matlab detects you're indecing to an element outside the current bounds of the matrix/array, it will automatically pad the missing elements with zeros:

>> clear b; b(10) = 5 
b =
    0     0     0     0     0     0     0     0     0     5

此功能既非常有用,又非常危险.这对于使事实声明变得非常容易非常有用,例如您自己的情况.您可以通过发出类似

This feature is both very useful, and very dangerous. It is useful for the fact declarations can be made very easy, such as your own case. You can create a whole array of custom-made classes by issuing something like

myClassArray(500) = myClass(1, 2);

这比类似的东西无限好

% cannot pre-allocate (zeros() or ones() give double/uint8/..., not myClass)
for ii = 1:499
     myClassArray(ii) = myClass; % so, growing array
end
myClassArray(500) = myClass(1,2);

但是,很难发现不断增长的阵列:

But, growing arrays can be hard to spot:

a = zeros(10,1);
for ii = 1:10
    a(ii+1) = rand;
end

这会使性能大大下降.同样,当您将Matlab中原型化的代码转换为静态类型的语言(如C ++)时,复制此代码将导致缓冲区溢出,从而导致段错误.

which can make performance drop tremendously. Also, when you translate code prototyped in Matlab to a statically-typed language like C++, copying this code will result in buffer overflows and thus segfaults.

现在,回到您的情况:

clear a;   a(1:2:5) = 1:-4:-7 

1:2:5将扩展为数组[1 3 5],而1:-4:-7将给出值[1 -3 -7].由于变量a尚不存在,Matlab将创建一个新变量,并用值[1 -3 -7]填充元素[1 3 5].为了初始化变量a(即[2 4])而被跳过的索引将被自动初始化为零.

The 1:2:5 will expand to the array [1 3 5], and the 1:-4:-7 will give the values [1 -3 -7]. Since the variable a does not exist yet, Matlab will create a new one and fill the elements [1 3 5] with the values [1 -3 -7]. The indices that have been skipped in order to initialize variable a (namely, [2 4]) will then have been initialized automatically to zero.

如果您熟悉Python,就有点像将多个值分配给多个变量的语法

If you're familiar with Python, it's a bit like the syntax to assign multiple values to multiple variables

x,y = 1,2

但是在您的Matlab情况下,这些不同的变量是不存在的数组的索引,这需要用某些东西填充孔"以使其成为有效的一致数组.

But in your Matlab case, these different variables are indices to a non-existent array, which requires "filling the holes with something" to make it a valid, consistent array.

这可以使事情变得清楚吗?

Does this make things clear?

这篇关于MATLAB中冒号运算的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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