Search code examples
matlaboperators

Can matlab colon operator be used with arithmetic operators?


Let's say I want to make a vector:

A = [4 8 16 32]

Is there any way to do this using the colon operator ? For example something like:

A = 4:(*2):32;

Solution

  • No, this is not possible in Matlab. You can use it like @Luis showed:

    A = 2.^(2:5);
    

    Or if you want to do this with a different function in the future:

    A = [];
    for n = 2:5
        A = [A n^2];
    end
    

    By changing the limits of the for loop and the n^2 part to your desired values, you can do it however you like.

    Hope this helps.