MATLAB Indexing Conventions for Vectors / 1D-Arrays

Viewed 77

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 (e.g. 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
2 Answers
Related