functions not working for a matrix created in for loop.

1 visualizzazione (ultimi 30 giorni)
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 Commento
Dyuman Joshi
Dyuman Joshi il 7 Mag 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.

Accedi per commentare.

Risposta accettata

Riccardo Scorretti
Riccardo Scorretti il 7 Mag 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))

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by