I am trying to create a nested loop from a random matrix and display the max value of each row and each column without using max function.
조회 수: 2 (최근 30일)
이전 댓글 표시
%it has to have user inputs for rows and columns, and a random generation of the matrix, which i have already. I am having problems getting the max value of each separate row and column, and it will not loop through each row and column.
rows=input('Enter the number of rows: '); cols=input('Enter the number of columns: '); rng('shuffle') matran=randi(100,rows,cols) maxrow=matran(1:1); maxcol=matran(1:1); for row = 1:rows if matran(row) > maxrow maxrow= matran (row);
end
for col = 1:cols
if matran(col)> maxcol
maxcol=matran(col);
end
end
end
댓글 수: 0
답변 (1개)
BhaTTa
2024년 7월 23일
Here is the code to get max value from each row and column:
rows = input('Enter the number of rows: ');
cols = input('Enter the number of columns: ');
rng('shuffle');
matran = randi(100, rows, cols);
% Initialize arrays to store max values for each row and column
max_row_values = zeros(1, rows);
max_col_values = zeros(1, cols);
% Find max values for each row
for row = 1:rows
max_value = matran(row, 1);
for col = 1:cols
if matran(row, col) > max_value
max_value = matran(row, col);
end
end
max_row_values(row) = max_value;
end
% Find max values for each column
for col = 1:cols
max_value = matran(1, col);
for row = 1:rows
if matran(row, col) > max_value
max_value = matran(row, col);
end
end
max_col_values(col) = max_value;
end
disp('Matrix:');
disp(matran);
disp('Max values of each row:');
disp(max_row_values);
disp('Max values of each column:');
disp(max_col_values);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!