Writing my own function to find one-norm

1 回表示 (過去 30 日間)
Emily Gallagher
Emily Gallagher 2019 年 10 月 15 日
回答済み: Jalaj Gambhir 2019 年 10 月 18 日
I am trying to write a function called normone that accepts a rectangular matric as a parameter and calculates the one-norm. I know this can be done by norm(A,1) but I am trying to write my own function. Below is my code but it is not giving me the correct answer.
function f = normone
seed = 7;
rng(seed)
A = rand(n,n);
f = norm(A, 1);
end

回答 (1 件)

Jalaj Gambhir
Jalaj Gambhir 2019 年 10 月 18 日
Hi,
If you want the function to accept the matrix as a parameter, you need:
function f = normone(A)
f = norm(A,1);
end
You can call this function by:
>> A = rand(3)
A =
0.4149 0.7094 0.9555
0.0014 0.5243 0.6829
0.0923 0.6962 0.0531
>> normone(A)
ans =
1.9299
Other alternative to write a function similar to what you mentioned in the question is:
function f = normone(n)
seed = 7;
rng(seed);
A = rand(n,n);
f = norm(A,1);
end
which can be called using:
>> normone(4)
ans =
2.2513

カテゴリ

Help Center および File ExchangePartial Differential Equation Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by