Search code examples
matlabmathoctavenumerical-methods

Create a specific number pattern using matlab / octave


How can I create a specific number pattern? Where the last number starts the 2nd pattern section.

Example:

I want to follow / repeat the pattern 1,5,4

1st pattern in section 1,5,4
2nd pattern in section 5,9,8
3rd pattern in section 9,13,12

final array would be = [1,5,4,5,9,8,9,13,12,...]

note: this is just a simple example the pattern will be about 100,000 + numbers

I know about repmat and cumsum but combining these two didn't work

PS: I'm using Octave 4.2.2 which is similar to Matlab


Solution

  • x=reshape([1;5;4]+[0:4:n*4],1,[])
    

    When you take a look at your question, each row is [1;5;4] incremented by a multiple of 4. That is what above code does, adds [1;5;4] with a multiple of 4. Finally the result is reshaped to a vector.