Search code examples
matrixoctavearray-broadcasting

Create a matrix A = [4 ... 128] in Octave language


Can you help to know how to create a matrix in Octave using a shortened way?

I need to have (matrix) A = [4, 8, 16, 32, 64, 128]; Want to use something like A = [4: *2 : 128] (meaning start = 4, step = *2 : finish = 128), but this doesn't work in Octave.

The same needs to be done to matrix B = [1 4 9 16 25 36], where step is 3 at the beginning and is increasing by 2 on the next step.

Any ideas?


Solution

  • With the colon operator you can only do steps of the same size. But notice that your matrix

    A = [4, 8, 16, 32, 64, 128];
    

    has the structure [2^2, 2^3, 2^4, ..., 2^7], so you can make use of broadcasting and define it as

    A = 2.^[2,3,4,5,6,7];
    

    or simply

    A = 2.^(2:7);