MATLAB Answers

Why does my matrix give me a syntax error

2 views (last 30 days)
Eric Hofmann
Eric Hofmann on 11 Sep 2021
Edited: Image Analyst on 12 Sep 2021
%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 Comments
John D'Errico
John D'Errico on 12 Sep 2021
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.

Sign in to comment.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 12 Sep 2021
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 Comment
Image Analyst
Image Analyst on 12 Sep 2021
@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.

Sign in to comment.


Matt J
Matt J on 12 Sep 2021
(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.

Community Treasure Hunt

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

Start Hunting!