Why does my matrix give me a syntax error

3 vues (au cours des 30 derniers jours)
Eric Hofmann
Eric Hofmann le 11 Sep 2021
Modifié(e) : Image Analyst le 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 commentaires
Image Analyst
Image Analyst le 12 Sep 2021
Modifié(e) : Image Analyst le 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 le 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.

Connectez-vous pour commenter.

Réponses (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 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 commentaire
Image Analyst
Image Analyst le 12 Sep 2021
Modifié(e) : Image Analyst le 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.

Connectez-vous pour commenter.


Matt J
Matt J le 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.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by