Change elements by a set amount.

조회 수: 1 (최근 30일)
Jay
Jay 2014년 10월 19일
댓글: Image Analyst 2014년 10월 20일
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개)

카테고리

Help CenterFile Exchange에서 Detection에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by