How to use negative index ranges in for loop?

91 views (last 30 days)
I heard about matlab doesn't support negative indexes in the loops. Is there any other way to use negative index in the for loop?.
I can use for loop as "for n=1:10" Is it possible to use for loop as "for n=-5:5" ?
  2 Comments
Ron Bremner
Ron Bremner on 21 Dec 2020
There is another option, which comes close to using negative indices. In one instruction, it maps negative indices to unused positive indices. As an example:
a=[1:11] % Initialize a as a vector with 11 elements
for i = [-5:-1 1:5]
b=a(mod(i,11)) % This copies the ith element of a into b, if i is positive,
% and copies the 11 - ith element of a into b if i is negative
[i a] % This prints out i (the index) and the contents of a
end
Notes:
  1. i cannot be 0, as 0 is mapped to index value 0, which does not work in Matlab. There are workarounds for this, but they add more complexity than my problems are worth
  2. The array should be initialize to be as large as the largest positive index, - the smallest negative (most negative) index.
Image Analyst
Image Analyst on 22 Dec 2020
Ron, can you please make this comment into an official Answer by posting it down below in the official Answers section rather than up here in comments, which is used to ask the poster to clarify his question. You can also get credit in terms of "reputation points" if he accepts your answer or someone votes for your answer.

Sign in to comment.

Answers (3)

drummer
drummer on 19 Apr 2020
It's not possible.
if you want to run a for loop like you showed in your example ranging from -5 to + 5
By doing like this, you're gonna get an error.
for i = -5:5
array(i) = i
end
Because you can never get array(-5), array(-4) and so on in order to show the element location. Because indexes must be positive integers.
Fortunately, you can adjust your indexes to mark up the element location as previously reported by other fellows:
for i = -5 : 5
array(i + 6) = i
end
That way, notice that for i = -5, your array index will be array(-5+6). Which is array(1) and so on...

KSSV
KSSV on 21 Dec 2016
Yes you can use for loop like that...
for i = -5:1:5
i
end
But remember that, you cannot use this i with -ve values as a index to call element of array/ matrix.
  2 Comments
Tamil selvan
Tamil selvan on 21 Dec 2016
Thanks for the info. Another question came to mine mind! Please see the attached file.
Assume its need to use the i variable as -5 to 4 and have to access the corresponding index of input buffer. But in matlab we can't handle negative indexes in matrix or arrays. However, I would like to use inbuilt ifft block. Please let me know is their any way.
KSSV
KSSV on 21 Dec 2016
See the below example:
i = -5:4 ;
j = 1:10 ;
input = rand(1,10) ;
[i ; j ;input]
i corresponds to what shown in your attahced formula, j is the respective indices in MATLAB. You have to manipulate them depending on how many elements are present in your input. In your case i is from -5 to 4, so there are 10 elements in input, which would be from 1 to 10 i.e j in matlab.

Sign in to comment.


Image Analyst
Image Analyst on 21 Dec 2016
You can do things like this:
for n=-5:5
yourArray(n+6) = rand;
end
or
index = 1
for n = -123.45 : 0.345 : 789.1448
yourArray(index) = rand
index = index + 1;
end
You just have to know that the starting index (1) of your array was when your n was the starting n value, the element #2 was the next n value, and so on.
  2 Comments
Tamil selvan
Tamil selvan on 21 Dec 2016
Yes I too worked on it. The problem is while using inbuilt ifft Matlab function. In ifft matlab function, we can pass only the array and fft length. However, I need to use the i values from -5 to 5. In default matlab take i value as 0 to 10.
Please let me know how to use inbuilt fft matlab function for this scenario.
KSSV
KSSV on 21 Dec 2016
You need not to worry about it's indices....input the array you want into the function.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!