Search code examples
matlaboctavenumerical-methodssequencessequential

Numerical number pattern. and if then else in octave / matlab


I'm trying to get a numerical repeating number pattern like this 1,2,3,4,1,2,3,4,1,2,3,4... I'm using Octave 3.8.1 which is like matlab

I tried

t=20 %note this number will vary 
a=mod(x,5)

but a is 1,2,3,4,0,1,2,3,4,0,1,2,3,4

I know I can do odd and even numbers using the code below

for ii=1:20 %note this number will vary 
    if mod(ii,2)==0
                %number is even
    else
                %number is odd
    end
end

But how can I do it with 4 separate number choices of 1,2,3,4


Solution

  • >> t=20 %note this number will vary
    t =  20
    >> maxValue=4; %repeat the numbers 1..maxValue
    >> x=0:t-1 %I think this is what you meant to do...
    x =
    
        0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19
    
    >> a=mod(x,maxValue)+1 %cycle through 0..3 and add 1
    a =
    
       1   2   3   4   1   2   3   4   1   2   3   4   1   2   3   4   1   2   3   4