How to produce a matrix with the following conditions?

조회 수: 1 (최근 30일)
M
M 2022년 9월 13일
답변: Walter Roberson 2022년 9월 15일
How to produce a matrix which size is 7*7 and contains all possible single locations of a certain value and the rest of the column's values are 1. For example : The value is 0.4 and I want to produce seven columns of different single locations of 0.4 and the other values are 1 such as: [0.4 1 1 1 1 1 1; 1 0.4 1 1 1 1 1; 1 1 0.4 1 1 1 1; 1 1 1 0.4 1 1 1; 1 1 1 1 0.4 1 1; 1 1 1 1 1 0.4 1; 1 1 1 1 1 1 0.4]

채택된 답변

Sam Chak
Sam Chak 2022년 9월 13일
Hi @M
Maybe like this?
M = ones(7)
M = 7×7
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I = eye(7)
I = 7×7
1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
v = 0.4;
u = repelem(v, 7)
u = 1×7
0.4000 0.4000 0.4000 0.4000 0.4000 0.4000 0.4000
D = diag(u)
D = 7×7
0.4000 0 0 0 0 0 0 0 0.4000 0 0 0 0 0 0 0 0.4000 0 0 0 0 0 0 0 0.4000 0 0 0 0 0 0 0 0.4000 0 0 0 0 0 0 0 0.4000 0 0 0 0 0 0 0 0.4000
Solution = M - I + D
Solution = 7×7
0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000
  댓글 수: 2
Les Beckham
Les Beckham 2022년 9월 13일
Or, with a few less steps:
M = ones(7);
M(logical(eye(7))) = 0.4
M = 7×7
0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000
Sam Chak
Sam Chak 2022년 9월 13일
Hi @Les Beckham, thanks for introducing the logical approach. 👍

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 9월 15일
M = ones(7);
C = M;
C(logical(eye(7))) = 0.4
C = 7×7
0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000
%or
M = ones(7);
C = M;
C(1:size(C,1)+1:end) = 0.4
C = 7×7
0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000
%or
M = ones(7);
C = M - 0.6 * eye(size(M))
C = 7×7
0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.4000

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by