"end + 1"行在这里做什么? [英] What is the `end+1` line doing here?
问题描述
请考虑以下MATLAB代码:
Consider the following bit of MATLAB code:
degree = 6;
out = ones(size(X1(:,1)));
for i = 1:degree
for j = 0:i
out(:, end+1) = (X1.^(i-j)).*(X2.^j);
end
end
我不确定我看到end+1
索引如何运行.此代码中没有以前定义的名为end
的变量.
I'm not sure I see how the end+1
index is functioning. There are no previously defined variables called end
in this code.
推荐答案
Writing end
as a subscript is equivalent to writing the index of last element in the array in the specified dimension, as stated in the official documentation:
end
函数还用作索引表达式中的最后一个索引.
在这种情况下,end = (size(x,k))
用作第k
个索引的一部分.这种用法的示例是X(3:end)
和X(1,1:2:end-1)
.当使用end
增长数组时,如在X(end+1)=5
中一样,请确保首先存在X
.
The
end
function also serves as the last index in an indexing expression.
In that context,end = (size(x,k))
when used as part of thek
th index. Examples of this use areX(3:end)
andX(1,1:2:end-1)
.When usingend
to grow an array, as inX(end+1)=5
, make sureX
exists first.
在您的情况下(以粗体突出显示),out(:, end+1)
表示矩阵out
在i
的每次迭代中都在第二维中增长.
In your case (highlighted in bold), out(:, end+1)
means that the matrix out
is growing in the second dimension with each iteration of i
.
这篇关于"end + 1"行在这里做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!