user3454983
user3454983

Reputation: 1

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.

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

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

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

Divakar
Divakar

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

Related Questions