How can I replace values in a Matrix?
1 次查看(过去 30 天)
显示 更早的评论
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 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!