Displaying output as a vector?

13 ビュー (過去 30 日間)
Matthew Quinones
Matthew Quinones 2014 年 4 月 18 日
コメント済み: Image Analyst 2014 年 4 月 18 日
I am using a for loop to find the squares of the numbers 1-5. However, my answer needs to be in a 5 element vector. When I run it, it shows each individual answer like so:
x=1
x=4
x=9
x=16
x=25
I need to find a way to have it shown as a 5 element vector such as: x= 1 4 9 16 25
Any help?!?!

採用された回答

Image Analyst
Image Analyst 2014 年 4 月 18 日
for k = 1 : 5
x(k) = k^2;
end
or, in a more MATLAB-ish way
k=1:5;
x = k.^2;
or even more compact
x=(1:5).^2
  7 件のコメント
Matthew Quinones
Matthew Quinones 2014 年 4 月 18 日
ok so i have
x=1;
while x<5
y(k)=x^2;
x=x+1;
end
y
The output is
y=
16 0 0 0 16 ???
Image Analyst
Image Analyst 2014 年 4 月 18 日
Make it y(x) and see what it does.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by