functions not working for a matrix created in for loop.

1 vue (au cours des 30 derniers jours)
Nawaf Aldhawi
Nawaf Aldhawi le 7 Mai 2022
n = input('Enter number of rows: ');
m = input('Enter number of columns: ');
disp('Enter the values of the matrix: ');
sum = 0;
for i = 1:n
for j = 1:m
Mat(i,j) = input(' ');
sum = sum + Mat(i,j);
end
end
for i = 1:n
for j = 1:m
Matrix = zeros(i,j);
end
end
for i = 1:n
for j = 1:m
Matrix(i,j) = Mat(i,j);
end
end
max = max(max(Matrix))
as you can see here when i run the code, it give me an erorr for the [ max(max(Matrix)) ] function that goes 'Index exceeds the number of array elements (1).'
Thanks in advance!
  1 commentaire
Dyuman Joshi
Dyuman Joshi le 7 Mai 2022
What's the point of 2nd and 3rd For loops? Both are redundant.
Also, It's best not to use inbuilt function names as variable names - max in this case. Use maximum or something else.

Connectez-vous pour commenter.

Réponse acceptée

Riccardo Scorretti
Riccardo Scorretti le 7 Mai 2022
It gives that error the second time you run the code; the very first time it works nice. The problem is that in the last line:
max = max(max(Matrix))
you define a variable max, which "shadows" the homonimous function (= after that, you cannot use the function max anymore, until a variable with the same name exists).
The solution is to rename that variable, for instance:
max_ = max(max(Matrix))
  2 commentaires
Nawaf Aldhawi
Nawaf Aldhawi le 7 Mai 2022
oh my god lol, thank u so much!!!!!!!!
Riccardo Scorretti
Riccardo Scorretti le 7 Mai 2022
You are welcome.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by