How can I replace values in a Matrix?

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 日

0 投票

% 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

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

質問済み:

2023 年 2 月 27 日

コメント済み:

2023 年 2 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by