简体   繁体   中英

Imaginary number in loops with 'i' as variable in MATLAB

I have code with a loop in MATLAB , with i is the variable in the iteration:

for i = 1:n
    mycomplexexponential = exp(2*i);
    ....
end

The variable i overrides the imaginary number i inside of the loop. I want the i for mycomplexexponential to refer to the imaginary number.

The problem could be avoided by simply renaming the variable

for ii = 1:n
    mycomplexexponential = exp(2*i);
    ....
end

But for generic reasons, I need to keep the name of the variable as 'i'. How can I do this?

Lower case j can also be used for the imaginary unit in MATLAB . So your code would be

for i = 1:n
    mycomplexexponential = exp(2*j);
    ....
end

MATLAB convention states you do not use the multiplication operator with imaginary number symbol.
Namely, you should write:

mycomplexexponential = exp(2i);

This should solve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM