functions not working for a matrix created in for loop.

2 次查看(过去 30 天)
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
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))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops 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