Kevin
Kevin

Reputation: 229

Matlab iteration through array

I would like to iterate trough an array and for each interation store a specific range...like this:

CompleteRange = [5; 34; 6; 34; 67; 4; 6; 234; 6; 26; 246; 31; 43];
RangeWidow = 3;

for m = 0 : CompleteRange -1

    Range = CompleteRange(m...RangeWindow...??

end

The array "Range" should be during the first iteration (m=0): 5; 34; 6. Or for example during the third iteration (m=2): 6; 234; 6.

Could you please complete the code line within the for loop?

Thanks for your help!

Edit 1 as requested, expected Output:

Range: 5 
       34
       6
Range: 34
       67
       4
Range: 6
       234
       6
Range: 26
       246
       31

Upvotes: 0

Views: 746

Answers (2)

Tal Darom
Tal Darom

Reputation: 1409

I guess what you are looking for is:

for m = 1 : length(CompleteRange) - RangeWindow

   Range = CompleteRange(m:m+RangeWindow)

end

Since matlab arrays are 1 based and not 0 based I took the liberty to change the loop to start at 1.

Edit: If you want the steps to be of RangeWindow and not 1, replace

for m = 1 : length(CompleteRange) - RangeWindow

with:

for m = 1 : RangeWindow : length(CompleteRange) - RangeWindow

Upvotes: 1

Dan
Dan

Reputation: 45752

Your question is kind of unclear but what about just:

Range= reshape(CompleteRange, RangeWindow, [])'

This assumes that the length of completerange divides perfectly by rangewindow, if it doesn't then it's easy enough to just pad with NaNs

Upvotes: 1

Related Questions