Change elements by a set amount.

1 次查看(过去 30 天)
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 个评论
Jay
Jay 2014-10-20
编辑:Image Analyst 2014-10-20
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 2014-10-20
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.

请先登录,再进行评论。

采纳的回答

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

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by