Rick T
Rick T

Reputation: 3389

Getting numbers based on list of other numbers in matlab / octave

When I type in (1:4:16) in matlab / octave I get 1,5,9,13 as the answer

Is there a way I can get the missing numbers instead?

so instead of getting 1,5,9,13 
I get 2,3,4,6,7,8,10,11,12,14,15,16

Upvotes: 2

Views: 47

Answers (1)

Shai
Shai

Reputation: 114866

you can use this function:

function num = getTheMissingNumbers( from, jump, to )

num = from:to;
num = setdiff( num, from:jump:to );

You can call this function

>> getTheMissingNumbers( 1, 4, 16 )

to get the numbers you want.


If you further assume the input to getThemissingNumbers always starts with 1, you can implement it even more efficiently using

function num = getTheMissingNumbers( jump, to )

num = 1:to;
num(1:jump:to) = []; % remove the elements in ind

EDITED according to comment by tmpearce.

Upvotes: 2

Related Questions