functions not working for a matrix created in for loop.

1 回表示 (過去 30 日間)
Nawaf Aldhawi
Nawaf Aldhawi 2022 年 5 月 7 日
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 件のコメント
Dyuman Joshi
Dyuman Joshi 2022 年 5 月 7 日
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.

サインインしてコメントする。

採用された回答

Riccardo Scorretti
Riccardo Scorretti 2022 年 5 月 7 日
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 件のコメント
Nawaf Aldhawi
Nawaf Aldhawi 2022 年 5 月 7 日
oh my god lol, thank u so much!!!!!!!!
Riccardo Scorretti
Riccardo Scorretti 2022 年 5 月 7 日
You are welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by