Why does my matrix give me a syntax error

조회 수: 3 (최근 30일)
Eric Hofmann
Eric Hofmann 2021년 9월 11일
편집: Image Analyst 2021년 9월 12일
%first created function to make an equation for all the calculations
function Fn = Fnorm(A)
%the matrix's space is now set
[m,n] = size(A);
%set sum = to 0 first to allow for a fresh start
sum = 0;
%for the amount of spaces in the matrix,
% the forbenius norm will account for the sum
for i=1:m
for j=1:n
sum = sum + (A(i,j)*A(i,j));
end
end
Fn = sqrt(sum);
%matrix A is defined
A = [5 7 9; 1 8 4; 7 6 2];
fprintf ('The Forbenius Norm of the Matrix A is:\n')
end
  댓글 수: 4
Image Analyst
Image Analyst 2021년 9월 12일
편집: Image Analyst 2021년 9월 12일
What did you pass in for A?
Show us the exact line of code where you called the function.
You didn't just press the green run triangle without passing in A did you?
Why do you pass in A but then set it later with this line
A = [5 7 9; 1 8 4; 7 6 2];
but then never use that A? Don't overwrite the A that the user passes in with some A from inside the function.
John D'Errico
John D'Errico 2021년 9월 12일
Your problem is clearly you don't know how to spell Frobenius. :)
https://mathworld.wolfram.com/FrobeniusNorm.html#:~:text=The%20Frobenius%20norm%2C%20sometimes%20also%20called%20the%20Euclidean,can%20also%20be%20considered%20as%20a%20vector%20norm.

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

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 9월 12일
Note that [A] is not to be defined within Fnorm that should be called correctly. It works without any err, if you call your fcn with this:
A = [5 7 9; 1 8 4; 7 6 2];
Fn =Fnorm(A)
The Forbenius Norm of the Matrix A is:
Fn = 18.0278
function Fn = Fnorm(A)
%the matrix's space is now set
[m,n] = size(A);
%set sum = to 0 first to allow for a fresh start
sum = 0;
%for the amount of spaces in the matrix,
% the forbenius norm will account for the sum
for i=1:m
for j=1:n
sum = sum + (A(i,j)*A(i,j));
end
end
Fn = sqrt(sum);
% matrix A is provided while callin this fcn: Fnorm(A)
% No need to defined [A] here!
fprintf ('The Forbenius Norm of the Matrix A is:\n')
end
  댓글 수: 1
Image Analyst
Image Analyst 2021년 9월 12일
편집: Image Analyst 2021년 9월 12일
@Eric Hofmann, in other words, you need
A = [5 7 9; 1 8 4; 7 6 2];
Fn =Fnorm(A)
function Fn = Fnorm(A)
%code
end
NOT
function Fn = Fnorm(A)
%code
end
A = [5 7 9; 1 8 4; 7 6 2];
Fn =Fnorm(A)
because the script must come first in a file, then the function definitions. You can't have function definitions first, then the script after them.
Attach your actual m-file with the paperclip icon if you still need help.

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


Matt J
Matt J 2021년 9월 12일
(It follows the END that terminates the definition of the function "Fnorm".)
That is not allowed. All local functions must be placed at the end of the mfile and any script commands not contained in a function must be at the beginning.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by