在Matlab中使用i和j作为变量 [英] Using i and j as variables in Matlab

查看:580
本文介绍了在Matlab中使用i和j作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ij是非常流行的变量名称(请参见例如这个).

i and j are very popular variable names (see e.g., this question and this one).

例如,在循环中:

for i=1:10,
    % do something...
end

作为矩阵的索引:

mat( i, j ) = 4;

为什么不应该在Matlab中将它们用作变量名?

Why shouldn't they be used as variable names in Matlab?

推荐答案

因为ij都是表示的函数虚构单位 :

Because i and j are both functions denoting the imaginary unit:

  • http://www.mathworks.co.uk/help/matlab/ref/i.html
  • http://www.mathworks.co.uk/help/matlab/ref/j.html

因此,名为ij的变量将覆盖它们,可能会悄无声息地破坏执行复杂数学运算的代码.

So a variable called i or j will override them, potentially silently breaking code that does complex maths.

可能的解决方案包括改为使用iijj作为循环变量,或者在需要i代表虚部时使用1i.

Possible solutions include using ii and jj as loop variables instead, or using 1i whenever i is required to represent the imaginary unit.

这篇关于在Matlab中使用i和j作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆