How do I place function outputs into an array?

4 ビュー (過去 30 日間)
Xiuchen Han
Xiuchen Han 2020 年 9 月 6 日
コメント済み: Xiuchen Han 2020 年 9 月 6 日
Hi everyone
I am a relative beginner with MATLAB and I am trying to write a function which takes one input integer 'x', and returns an array of the squares of all numbers from 1 to x.
Here is my function so far:
function [y] = squaresarray (x);
for i=1:x;
y=i^2
end
When I try to call the function and insert a random value of x (e.g. 4), I just get all the squared values returned as multiple answers for y. How can I place these into a 1 dimensional array so that all the outputs are placed into one row?
Thank you

採用された回答

David Hill
David Hill 2020 年 9 月 6 日
function y = squaresarray(x)
y=(1:x).^2;
end
  1 件のコメント
Xiuchen Han
Xiuchen Han 2020 年 9 月 6 日
Thank you sir!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by