Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a vector to an empty MATLAB matrix

Tags:

I've got MATLAB code inserting n-dimensional points (n >1) into a matrix (myPointMatrix) and am having thoughts about how to insert the first point.

Right now the program checks the size of myPointMatrix before inserting a point. If it is 1x1, myPointMatrix is set equal to the current point. Otherwise the current point is appended. This if-statement is only true once, but is evaluated each time I insert a point, which is very very often.

Removing the if and trying to append to myPointMatrix makes MATLAB understandably complain about matrix dimensions not being consistent. Removing both the if-statement and the inialization of myPointMatrix = 0 causes MATLAB to find myPointMatrix undefined. Also understandable.

How do I initialize myPointMatrix so that I can remove the if-statement? Or is there some other smart solution?

myPointMatrix = 0;
for x=0:limit
    for y=0:limit
        for z=0:limit
            tempPoint = [x y z];
            if (length(myPointMatrix) == 1)
                myPointMatrix = tempPoint;
            else
                myPointMatrix = [myPointMatrix; tempPoint];
            end
        end
    end
end