MATLAB Indexing for Vectors / 1D Arrays

Consider the distribution of the following two vectors:

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

      

Now the goal is to assign values ​​to these vectors (for example, inside a loop if vectorization is not possible). Is there a convention or best practice for 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 like this?

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

      

+3


source to share


2 answers


Functionally, it doesn't matter. If the context means vectors are always 1D (your naming convention helps in this example), you can just use vecCol(i)

for brevity and flexibility. However, there are several advantages to using the syntax vecCol(i,1)

:



  • It clearly defines what type of vector you are using. It's okay if it matters, for example. when using linear algebra, but may be inappropriate if the direction is arbitrary.
  • If you forgot to initialize (bad, but it will) then it will ensure that the direction is as expected
  • It's a good habit to get in so you don't forget when using 2D arrays
  • It seems to be a little faster. This will be negligible on small arrays, but see below benchmark for vectors with elements 10^8

    and> 10% speed improvement.

    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
    
          

    Conclusion (tested on 64-bit Windows R2015b, your mileage may vary!)

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

    By repeating this test magnified n

    , we can obtain the following graph for reference.

    enter image description here

+6


source


The general rule of thumb in programming is "explicit is better than implicit". Since there is no functional difference between the two, I would say it depends on the context, which is cleaner / better:



  • if a lot of matrix algebra is used in the context and it is important to distinguish between row and column vectors, use two-argument indexing to reduce errors and make reading easier

  • unless the context is much different from the two, and you are just using vectors as simple arrays, using 1-argument indexing is cleaner.

+4


source







All Articles