Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

Using i and j as variables in MATLAB

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

For example, in loops:

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

As indices into a matrix:

mat(i, j) = 4;

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...