- 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
- The array should be initialize to be as large as the largest positive index, - the smallest negative (most negative) index.
How to use negative index ranges in for loop?
57 views (last 30 days)
Show older comments
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
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:
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.
Answers (3)
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...
0 Comments
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
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.
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
KSSV
on 21 Dec 2016
You need not to worry about it's indices....input the array you want into the function.
See Also
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!