Why does my matrix give me a syntax error

%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 Kommentare

Matt J
Matt J am 11 Sep. 2021
Bearbeitet: Matt J am 11 Sep. 2021
Why does my matrix give me a syntax error
The problem with not copy/pasting your error messages into your posts is that someone else running your code may see something quite different and not know what you're talking about:
Fnorm(eye(3))
The Forbenius Norm of the Matrix A is:
ans = 1.7321
Eric Hofmann
Eric Hofmann am 11 Sep. 2021
Error: File: Fnorm.m Line: 21 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "Fnorm".)
this is the error message
Image Analyst
Image Analyst am 12 Sep. 2021
Bearbeitet: Image Analyst am 12 Sep. 2021
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 am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

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 Kommentar

Image Analyst
Image Analyst am 12 Sep. 2021
Bearbeitet: Image Analyst am 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.

Melden Sie sich an, um zu kommentieren.

Matt J
Matt J am 12 Sep. 2021

0 Stimmen

(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.

Kategorien

Mehr zu Functions finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 11 Sep. 2021

Bearbeitet:

am 12 Sep. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by