Naming this as a function (beginner)

Existing code:
n=3
reshape(randperm(n^2),n,n)
_____________________
This one line of code does exactly what I want. Depending on the value of n; a matrix of n size produces unique element values between 1-n^2.
I'd like to call this function something; randomMatrix(n), so that I can just change the 'n' to do everything above. I'm not sure if I'm asking this clearly, so I'll quote the question from my homework.
I'm told to 'write a function called randomMatrix(n)' that does exactly what my eisting code does, I essentially just need to call the function by that name.
note: I've tried something like
function A = randomMatrix(n)
A = reshape(randperm(n^2),n,n)
end.
May someone help me out /annotate their code?

1 件のコメント

Benjamin Thompson
Benjamin Thompson 2022 年 2 月 16 日
Your code looks fine except you do not add a period after the "end" keyword and you should put a semicolon after the reshape line to suppress MATLAB displaying the value of A to the command window. What is wrong?

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

製品

リリース

R2021b

タグ

質問済み:

2022 年 2 月 16 日

コメント済み:

2022 年 2 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by