Change elements by a set amount.

1 view (last 30 days)
Jay
Jay on 19 Oct 2014
Commented: Image Analyst on 20 Oct 2014
I have a vector that goes up in twos:
delta= 2 2 4 4 6 6
What function can I use so that:
delta= 2 2 3 3 4 4 ?
  2 Comments
Jay
Jay on 20 Oct 2014
Edited: Image Analyst on 20 Oct 2014
Hi Mischa,
for i = 1:u(1,1)
if mod(delta(i,1),2) == 0
delta(i,1) = delta(i,1)
%number is even
else
delta(i,1) = delta(i+1,1)
%number is odd
end
delta= delta- delta/2 + 1
end
returns :
2
2
2.0625
2.09375
2.125
2.125
and not [2,2,3,3,4,4]
I have proceeded with some different code but the variable i does not update when the iteration runs through.
for i=1:6
%x_val = n
if x_val ==n
delta(i,1) = x_val
else %x_val~= n
x_val = n
delta(i,1) = x_val
end
% y_val to = x_val
if y_val==x_val
delta(i+1,1) = y_val
else
y_val=x_val
delta(i+1,1) = y_val
end
n=n+1
i=i+2
end
prior to the end of the command i returns the value of 3 for the first iteration but for the second iteration it reverts back to the count 2.
Image Analyst
Image Analyst on 20 Oct 2014
He didn't say to do all that stuff. You added code that broke it. Just do what he said. Like I did:
delta= [2 2 4 4 6 6]
delta = delta - delta/2 + 1
In the command window you'll see
delta =
2 2 4 4 6 6
delta =
2 2 3 3 4 4
exactly as you asked for. Of course since you didn't specify what algorithm was to be used, just simply that you wanted a function to return [2,2,3,3,4,4], I was going to suggest this function:
function delta = JustinsFunction(delta)
delta = [2,2,3,3,4,4];
That also meets all your stated requirements, more than Mischa's actually because I actually created a function and he did not - he just used operators.

Sign in to comment.

Accepted Answer

Mischa Kim
Mischa Kim on 19 Oct 2014
Justin, do you mean
delta = delta - delta/2 + 1
?

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!