generating matrices from another matrix

1 visualizzazione (ultimi 30 giorni)
TADMON YAYA
TADMON YAYA il 6 Dic 2022
Risposto: Jan il 6 Dic 2022
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 ?

Risposte (2)

David Hill
David Hill il 6 Dic 2022
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 il 6 Dic 2022
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.

Community Treasure Hunt

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

Start Hunting!

Translated by