Umar
Umar

Reputation: 89

For loop irregular incremental limit

I have a vector 'A' and based on this vector 'for loop' and 'filter' will look like with constant increment as

 A= [1:1:500]'; % random         
for i= min(A):10:max(A);
    filter = A>=i  & A<=i+10 ; % upper limit here is according to constant increment
end

Now if I need to change 'i' with irregular increment, how 'filter' will be adapted according to 'i' as indicated in '???'.

for i= [(min(A):10:100),(100:20:200),(200:40:max(A))];
    filter = A>=i  & A<=i+??? ; % i do not know how to set upper limit here according to increment
end

Its all about to obtain 'filter' based on iterations. since'filter' is defined as

 filter = A>=i & A<=i+iterations 

So in case of non regular iterations how should I proceed? It can be explained further as if we expand 'i', it will look like as following

i= [(min(A):10:100),(100:20:200),(200:40:max(A))]

i =

  Columns 1 through 19

 1    11    21    31    41    51    61    71    81    91   100   120   140   160   180   200   200   240   280

  Columns 20 through 24

 320   360   400   440   480

now 'filter' should be 1 to 11 , 11 to 21,.....,100 to 120, 120 to 140, .....440 to 480.

Upvotes: 1

Views: 348

Answers (2)

sebastian
sebastian

Reputation: 9696

What about using a trivial loop index and then simply indexing to elements of the filter limits array?

E.g. like this:

helper = [(min(A):10:100),(100:20:200),(200:40:max(A))]
for i=1:numel(helper)-1;
    filter = A>=helper(i) & A<=helper(i+1)  ; % i do not know how to set upper limit here according to increment
end

As an aside, you might want to elaborate on the larger context of this. It seems this is similar e.g. to histogramming. This manual, loop-based approach might be replacable by something quicker/easier to read.

Upvotes: 1

Vuwox
Vuwox

Reputation: 2359

You can do something like that. But I'm sure there a better way.

DefautIter = 10;
OldI = -9;

for i= [(min(A):10:100),(100:20:200),(200:40:max(A))];
    iteration  = i - OldI;
    OldI = i;
    filter = A>=i  & A<=i+iteration  ; 
end

Upvotes: 0

Related Questions