0

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
Kevin
  • 229
  • 2
  • 10
  • 19
  • 1
    Please edit your question to add the complete output you are looking for in matrix form. You can calculate it manually I'm sure, but as it stands your question very unclear. – Dan Feb 26 '15 at 08:51
  • For every iteration, the Range "Window" should shift over the "CompleteRange". – Kevin Feb 26 '15 at 09:03
  • yes but do you want this as a 2D matrix? Or to just print it to the screen. Please change your `Range` variable to be an actually Matlab style datatype so we know what you want. – Dan Feb 26 '15 at 09:19

2 Answers2

1

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

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Sorry, I made a mistake. I don't have "SpecificRange". See update above. – Kevin Feb 26 '15 at 09:04
  • Adding NaN's to arrays is a really bad practice. It causes very big computation slowdown. – Tal Darom Feb 26 '15 at 09:24
  • @TalDarom without knowing what the OPs intentions are thee isn't anything else to suggest. Also efficiency was not mentioned as a criteria and if using NaNs adds 0.0001 seconds of overhead then I would say it is good practice. BTW I've never heard that NaN slows down an array in Matlab, do you have a link or example of this? – Dan Feb 26 '15 at 09:27
  • 1
    @TalDarom - Please take no offence, but I believe that is absolute nonsense. Do you have any proof that verifies your claim? – rayryeng Feb 26 '15 at 09:37
  • I didn't say it was completly wrong - just a (very) bad practice. As for slowdown caused by Nans - feel free to google it. – Tal Darom Feb 26 '15 at 09:39
  • 1
    @TalDarom - I did. That's why I'm curious. Can you provide a reference? – rayryeng Feb 26 '15 at 09:40
  • 1
    @TalDarom I googled it before I asked and found nothing... that's why I asked – Dan Feb 26 '15 at 09:41
  • @rayryeng Feel free to use NaNs as much as you want. If you want to know more you can google "nan slowdown" or nan slowdown matlab" and find lots of information on the subject. – Tal Darom Feb 26 '15 at 09:52
  • 1
    @TalDarom I'm sorry I didn't find anything. I typed in that exact query... Both of them actually... And they gave me only one link from the MathWorks forums from 2005. Hardly an authoritative reference as it's 10 years old. I'll leave it at this and chalk it up to apocryphal. Good luck! – rayryeng Feb 26 '15 at 09:59
  • @TalDarom Please post an actual link explaining or demonstrating this slowdown? Because I can't find anything on it either and if it is the case that NaNs cause slowdown I would like to know about it. But I'm not just going to go on your say so without some proof :) – Dan Feb 26 '15 at 10:02
  • This is not a matlab issue, but a processor issue. here is a pretty good explenation: http://stackoverflow.com/questions/9574192/should-i-use-floating-points-nan-or-floating-point-bool-for-a-data-set-that – Tal Darom Feb 26 '15 at 10:07
  • 1
    @TalDarom tried it in Matlab, it does not hold. I'm assuming that the matlab matrix objects have more overhead and probably already assume that NaNs need to be accounted for. Operations using then take the same amount of time as not using (from empirical testing). Just because it's a C++ issue does not make it a Matlab issue. Do you have any examples proving this to be ad practice in MATLAB? – Dan Feb 26 '15 at 10:14
  • Since you got me re-reading on the subject - it seems that starting from "sandy bridge" intel arch. the penalty for NaNs was greatly decreased. – Tal Darom Feb 26 '15 at 10:21
1

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
Tal Darom
  • 1,379
  • 1
  • 8
  • 26
  • But you overwrite `Range` at every iteration...? Also you are shifting by `1` element each time instead of `3` – Dan Feb 26 '15 at 09:21
  • I thought that's what the op meant. – Tal Darom Feb 26 '15 at 09:22
  • this was exactly what I was looking for. The "Range" variable i use for further processing (function parameter) within the for loop. The result of the called function will be stored in another variable. So everything is fine now. Thank you all! – Kevin Feb 26 '15 at 09:33
  • @Kevin if you want `Range` to actually match the output you provided, then rather loop like this: `or m = 1:RangeWindow:(length(CompleteRange) - RangeWindow)`, i.e. in steps of your window size so that your data don't overlap. – Dan Feb 26 '15 at 09:46