简体   繁体   English

预定义行矩阵的一个元素,其中其他元素通过方程式创建

[英]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.

相关问题 在Matlab中构造一个矩阵,其中每个元素都是通过将同一行中的所有其他元素相加而获得的 - Construct a matrix in Matlab where each element is obtained by summing all other elements in the same row 如何将某些元素合并到行矩阵的一个元素中 - how do I combine some elements into one element of a row matrix 如何将一行的元素与同一矩阵中的每一行进行比较 - How do I compare elements of one row with every other row in the same matrix 检查矩阵的每一行是否有一个元素 - Check if every row of a matrix has a one element 相对于矩阵中行的其他元素分配元素的等级 - Assigning ranks of elements relative to the other elements of a row in a matrix 如何求解这个 ode 方程,其中一个输入有一些延迟,方程以矩阵形式写在 MATLAB ode45 中? - How to solve this ode equation where one of the inputs has some delay and equation has written in matrix form in MATLAB ode45? 从矩阵的每一行中删除一个元素,每个元素在不同的列中 - Remove one element from each row of a matrix, each in a different column 我的3x3矩阵中的每个元素都是关于t的方程。 如何绘制每个人的时间? - Each element in my 3x3 matrix is an equation with respect to t. How do I plot each one with respect to time? MatLab:如果两个先前矩阵的行元素相同,则创建矩阵行元素 - MatLab: Create matrix row element if row elements of two previous matrices are identical 将一个数组中的每个元素与其他数组中的元素进行匹配而无需循环 - Match each element of one array with elements of other array without loops
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM