Creating tables from a program?
이전 댓글 표시
Ok the question was, Write the MATLAB statements required to calculate and print out the squares of all even integers from from 0 to 50. Create a table consisting of each integer and its square, with the appropriate labels over each column(right alligned.
In other words:
Integer Square
======= ======
0 0
2 4
. .
. .
50 2500
Here's my program:
cnt = 0;
for z = 0:2:50
cnt = cnt + 1;
x(cnt)=z^2;
fprintf('the interger %g has a square value of %g\n', z,x(cnt))
end
The fprintf was just so see if the program worked.
Thanks!
댓글 수: 2
Jan
2011년 3월 3일
Do you have a question?
Isaias Santiago
2014년 12월 5일
x = -2;
y = 0;
maxy = 2304;
limity = 2305;
fprintf ('integer square \n')
fprintf ('------- ------ \n')
while (maxy < limity)
x = x + 2;
y = x^2;
ynew = y;
maxy = ynew;
fprintf ('%3i %3i\n', x, ynew)
end
채택된 답변
추가 답변 (1개)
Sean de Wolski
2011년 3월 3일
You could also do:
z = (0:50).';
fprintf(' Integer, Integer^2\n-------------------\n');
disp([z,z.^2]);
댓글 수: 4
Matt Tearle
2011년 3월 3일
bonus points for the vectorization!
you could also control the display of the values using fprintf (again one vectorized line of code)
Sean de Wolski
2011년 3월 3일
I tried but couldn't get it to have the heading only once in a one liner?
Walter Roberson
2011년 3월 3일
Sean,
fprintf('Header\n%s', sprintf('%f %f\n', [z,z.^2].'))
Oleg Komarov
2011년 3월 8일
That's a trick...
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!