generating matrices from another matrix

1 回表示 (過去 30 日間)
TADMON YAYA
TADMON YAYA 2022 年 12 月 6 日
回答済み: Jan 2022 年 12 月 6 日
x = [1,0,0,1,0,1]
x = 1×6
1 0 0 1 0 1
d = diag(x)
d = 6×6
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1
I want to generate matrices from the matrix d so that it gives me the shares below;
Share 1:
1 1 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 1
Share 2:
1 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 1
Share 3:
1 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 1
etc.
But I don't know how to do it. Can somebody help me ?

回答 (2 件)

David Hill
David Hill 2022 年 12 月 6 日
x = [1,0,0,1,0,1];
d = diag(x);
s(:,:,1)=d;
for k=2:10
t=d;t(k)=1;
s(:,:,k)=t';
end
s
s =
s(:,:,1) = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,2) = 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,3) = 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,4) = 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,5) = 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,6) = 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,7) = 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,8) = 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,9) = 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 s(:,:,10) = 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1

Jan
Jan 2022 年 12 月 6 日
x = [1,0,0,1,0,1];
d = diag(x);
Share1 = d;
Share1(1, 2) = 1;
Share2 = d;
Share2(1, 3) = 1;
Share3 = d;
Share3(1, 4) = 1;
Or with a loop:
x = [1,0,0,1,0,1];
d = diag(x);
Share = repmat(d, 1, 1, 4);
for k = 1:3
Share(1, k+1, k) = 1;
end
Now Share(:, :, 2) is e.g. Share2 from the first method. Remember that hiding indices in the names of variables is a bad design.

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by