[英]Predefining one element of a row matrix where other elements are created via an equation
I have a matrix that is created via the equation 我有一个通过等式创建的矩阵
for xxx = 1 : xMid_p - 2
ln_p(1,xxx) = abs(radius_p(1,1) - radius_p(xxx+1));
end
However I need this equation to have zero as its first element. 但是,我需要将此方程式的第一个元素设为零。 I understand I can do this via 我知道我可以通过
ln_p(1,1) = 0;
But how do I combine this so as that the first element is zero and the rest of the row matrix is taken from the equation above. 但是,我该如何结合起来以使第一个元素为零,而行矩阵的其余部分则取自上式。
Solution: 解:
It was pretty simple and involved concatenating a simple 1x1 matrix with the ln_p matrix. 这非常简单,涉及将一个简单的1x1矩阵与ln_p矩阵连接在一起。
lnZero(1,1) = 0
for xxx = 1 : xMid_p - 2
ln_p(1,xxx) = abs(radius_p(1,1) - radius_p(xxx+1));
end
ln_p = horzcat(lnZero,ln_p)
Dirty trick maybe; 可能是肮脏的把戏; use this inside the loop - 在循环中使用它-
ln_p(1,xxx) = (xxx~=1).*(abs(radius_p(1,1) - radius_p(xxx+1)));
Just use vectorized indexing and simple horizontal concatenation: 只需使用向量化索引和简单的水平串联:
ln_p(1, 1:xMid_p-1) = [0 abs(radius_p(1)-radius_p(2:xMid_p-1))];
if your ln_p
is empty before the loop and radius_p
is exactly xMid_p-1
elements long, you can simplify this to: 如果您的ln_p
在循环之前为空,并且radius_p
恰好是xMid_p-1
元素长,则可以将其简化为:
ln_p = [0 abs(radius_p-radius_p(1))];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.