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

查看:27
本文介绍了在 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:

因此,名为 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天全站免登陆