Reputation: 1
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.
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)
Upvotes: 0
Views: 31
Reputation: 38032
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 = [0 abs(radius_p-radius_p(1))];
Upvotes: 0
Reputation: 221594
Dirty trick maybe; use this inside the loop -
ln_p(1,xxx) = (xxx~=1).*(abs(radius_p(1,1) - radius_p(xxx+1)));
Upvotes: 1