write a for loop to round up vector

조회 수: 1 (최근 30일)
jarvan
jarvan 2014년 11월 1일
댓글: jarvan 2014년 11월 1일
i have a vector = [123.231 232.123 123.565 123.543] I need to use for loop to give out an result as:
number 1 is 123.2 number 2 is 232.1 number 3 is 123.6 number 4 is 123.5
which is round up to 1 decimal place.
Here I got:
vec = [1.111 2.222 3.222 4.222];
for i=1:length(vec)
fprintf('Element %d is %5.1f \n',i,vec)
end
I cant see what's wrong with my code but it wasnt display want i need. Could anyone help me out? Thanks

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 1일
편집: Azzi Abdelmalek 2014년 11월 1일
you forget to use vec(i) instead of vec
vec = [1.111 2.222 3.222 4.222];
for i=1:length(vec)
fprintf('Element %d is %5.1f \n',i,vec(i))
end
  댓글 수: 1
jarvan
jarvan 2014년 11월 1일
it runs perfect right now. thanks!

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

추가 답변 (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