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
3
  • 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. Commented Feb 26, 2015 at 8:51
  • For every iteration, the Range "Window" should shift over the "CompleteRange". Commented Feb 26, 2015 at 9: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. Commented Feb 26, 2015 at 9:19

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

But you overwrite Range at every iteration...? Also you are shifting by 1 element each time instead of 3
I thought that's what the op meant.
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 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.
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

13 Comments

@TalDarom - Please take no offence, but I believe that is absolute nonsense. Do you have any proof that verifies your claim?
@TalDarom - I did. That's why I'm curious. Can you provide a reference?
@TalDarom I googled it before I asked and found nothing... that's why I asked
@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!
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.