How can I replace values in a Matrix?

조회 수: 1 (최근 30일)
Muhammed Ali
Muhammed Ali 2023년 2월 27일
댓글: Stephen23 2023년 2월 27일
Hello Folks,
i have this following problem: I want to replace all values in a Matrix, which arent in the diagonal with the number 1.
Can someone give me a solution?
  댓글 수: 1
Stephen23
Stephen23 2023년 2월 27일
That sounds like homework. What have you tried so far?

댓글을 달려면 로그인하십시오.

답변 (1개)

Nikhilesh
Nikhilesh 2023년 2월 27일
% Define example matrix
A = [1 2 3; 4 5 6; 7 8 9];
% Get dimensions of matrix
[nrows, ncols] = size(A);
% Replace non-diagonal values with 1
for i = 1:nrows
for j = 1:ncols
if i ~= j
A(i,j) = 1;
end
end
end

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by