MATLAB Answers

I don't know how to call my function

2 ビュー (過去 30 日間)
Joshua Vuong
Joshua Vuong 2021 年 9 月 7 日
コメント済み: Image Analyst 2021 年 9 月 8 日
function norm = myvectornorm(x)
% calculate the norm
s=0;
n=length(x);
for i=1:length(x)
s =s+ x*(i)^2;
end
norm= sqrt(s);
end

回答 (1 件)

John D'Errico
John D'Errico 2021 年 9 月 8 日
編集済み: John D'Errico 2021 年 9 月 8 日
What language are you using here? Possibly not MATLAB. :)
s = s + L*(i)^2;
Do you think that squares the number i, and then multiplies the square of that value by the vector L? After all, * tells MATLAB to multiply two things.
Or, did you want to access the i'th element of L, and then square it? But L is itself only the length of the vector x, so just the number of elements in x.
Or, do you relly want to access the i'th element of the vector x, square THAT, and then sum the result into s?
The code you wrote will not achieve the latter. For that to happen, you would need to write this:
s = s + x(i)^2;
  4 件のコメント
Image Analyst
Image Analyst 2021 年 9 月 8 日
Call it like this
x = [1,1,1]; % Whatever values you want.
norm = myvectornorm(x)

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

Community Treasure Hunt

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

Start Hunting!

Translated by