I don't know how to call my function

조회 수: 1 (최근 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
Joshua Vuong
Joshua Vuong 2021년 9월 8일
Well thanks I appreciate it but as I said in the title I don't know how to call my function its for an assignment asking me to do multiplt tests for different vecotrs one being [1 1 1] another [ 1/sqrt(2) 0 1/sqrt(2)] I might have communicated my complications incorrectly my code works fine. This is for matlab grader and I would appreciate the help with out the snide comments. thanks
Image Analyst
Image Analyst 2021년 9월 8일
Call it like this
x = [1,1,1]; % Whatever values you want.
norm = myvectornorm(x)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by