functions not working for a matrix created in for loop.

조회 수: 1 (최근 30일)
Nawaf Aldhawi
Nawaf Aldhawi 2022년 5월 7일
댓글: Riccardo Scorretti 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))

추가 답변 (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