generating matrices from another matrix

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Video capture and display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by