简体   繁体   English

矢量/ 1D阵列的MATLAB索引约定

[英]MATLAB Indexing Conventions for Vectors / 1D-Arrays

Consider the preallocation of the following two vectors: 考虑以下两个向量的预分配:

vecCol = NaN( 3, 1 );
vecRow = NaN( 1, 3 );

Now the goal is to assign values to those vectors (eg within a loop if vectorization is not possible). 现在的目标是为这些向量分配值(例如,如果无法进行矢量化,则在循环内)。 Is there a convention or best practice regarding the indexing? 是否有关于索引的约定或最佳实践?

Is the following approach recommended? 是否建议采用以下方法?

for k = 1:3
    vecCol( k, 1 ) = 1; % Row, Column
    vecRow( 1, k ) = 2; % Row, Column
end

Or is it better to code as follows? 或者编码如下更好?

for k = 1:3
    vecCol(k) = 1; % Element
    vecRow(k) = 2; % Element
end

It makes no difference functionally. 它在功能上没有区别。 If the context means that the vectors are always 1D (your naming convention in this example helps) then you can just use vecCol(i) for brevity and flexibility. 如果上下文意味着向量总是1D(在此示例中您的命名约定有帮助),那么您可以使用vecCol(i)来简化和灵活。 However, there are some advantages to using the vecCol(i,1) syntax: 但是,使用vecCol(i,1)语法有一些优点:

  • It's explicitly clear which type of vector you're using. 它明确清楚你正在使用哪种类型的矢量。 This is good if it matters, eg when using linear algebra, but might be irrelevant if direction is arbitrary. 这很重要,例如在使用线性代数时,如果方向是任意的,则可能无关紧要。
  • If you forget to initialise (bad but it happens) then it will ensure the direction is as expected 如果您忘记初始化(不好但发生了),那么它将确保方向符合预期
  • It's a good habit to get into so you don't forget when using 2D arrays 这是一个很好的习惯,所以你不要忘记使用2D数组
  • It appears to be slightly quicker. 它似乎稍微快一些。 This will be negligible on small arrays but see the below benchmark for vectors with 10^8 elements, and a speed improvement of >10%. 这在小阵列上可以忽略不计,但是对于具有10^8元素的矢量,并且速度提高> 10%,请参见以下基准。

     function benchie() % Benchmark. Set up large row/column vectors, time value assignment using timeit. n = 1e8; vecCol = NaN(n, 1); vecRow = NaN(1, n); f = @()fullidx(vecCol, vecRow, n); s = @()singleidx(vecCol, vecRow, n); timeit(f) timeit(s) end function fullidx(vecCol, vecRow, n) % 2D indexing, copied from the example in question for k = 1:n vecCol(k, 1) = 1; % Row, Column vecRow(1, k) = 2; % Row, Column end end function singleidx(vecCol, vecRow, n) % Element indexing, copied from the example in question for k = 1:n vecCol(k) = 1; % Element vecRow(k) = 2; % Element end end 

    Output (tested on Windows 64-bit R2015b, your mileage may vary!) 输出(在Windows 64位R2015b上测试,您的里程可能会有所不同!)

     % f (full indexing): 2.4874 secs % s (element indexing): 2.8456 secs 

    Iterating this benchmark over increasing n , we can produce the following plot for reference. 迭代此基准而非增加n ,我们可以生成以下图表以供参考。

    在此输入图像描述

A general rule of thumb in programming is "explicit is better than implicit". 编程中的一般经验法则是“显式优于隐式”。 Since there is no functional difference between the two, I'd say it depends on context which one is cleaner/better: 既然两者之间没有功能差异,我会说它取决于哪个更干净/更好的背景:

  • if the context uses a lot of matrix algebra and the distinction between row and column vectors is important, use the 2-argument indexing to reduce bugs and facilitate reading 如果上下文使用了大量的矩阵代数并且行和列向量之间的区别很重要,请使用2参数索引来减少错误并促进阅读

  • if the context doesn't disciminate much between the two and you're just using vectors as simple arrays, using 1-argument indexing is cleaner 如果上下文在两者之间没有太大的区别,并且你只是使用向量作为简单数组,那么使用单参数索引更清晰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM