In for loop, add same fractional value to all non-integers in the vector

조회 수: 1 (최근 30일)
Hello,
In vector A, I have values that are either integers or fractional values (all are X.3000). I'd like to add 0.2 to all fractional values, so that all non-integers are X.5000.
This is the code I have so far, and it's doing what I want it to, but only to the last value in the vector! What's the hiccough preventing it from being applied through the whole vector?
for i=length(A)
if floor(A(i)) ~= ceil(A(i))
A(i)=A(i)+.2;
end
end

채택된 답변

David Goodmanson
David Goodmanson 2017년 5월 8일
편집: David Goodmanson 2017년 5월 8일
HI balsip, try
for i = 1:length(A)
  댓글 수: 2
balsip
balsip 2017년 5월 8일
Yikes. The good news about asking a stupid question is that there's usually a simple answer. Thanks, David.
David Goodmanson
David Goodmanson 2017년 5월 8일
yes, we've all been there, where you look at something in plain sight and don't see it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by