How to use negative index ranges in for loop?

93 次查看(过去 30 天)
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 个评论
Ron Bremner
Ron Bremner 2020-12-21
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 2020-12-22
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.

请先登录,再进行评论。

回答(3 个)

drummer
drummer 2020-4-19
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 2016-12-21
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 个评论
Tamil selvan
Tamil selvan 2016-12-21
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 2016-12-21
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
Image Analyst 2016-12-21
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 个评论
Tamil selvan
Tamil selvan 2016-12-21
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 2016-12-21
You need not to worry about it's indices....input the array you want into the function.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by