How can I create a set of N diagnal matrices range from diag(1, 1, ... , 1) to diag(-1, -1, ... , -1) for testing purposes?

1 回表示 (過去 30 日間)
Zac Minson
Zac Minson 2021 年 9 月 20 日
編集済み: Matt J 2021 年 9 月 21 日
I am needing to create 16 4x4 diagnal matrices consisting of all of the combinations of "1" and "-1" and have them be able to be called for use in a loop. Besides creating each individual matrix, how can I go about accomplishing this?
for instance:
Z1 = diag(1,1,1,1);
.
Z8 = diag(1,-1,-1,1);
.
Z16 = diag(-1,-1,-1,-1);
Thank you for any guidance.
  3 件のコメント
Zac Minson
Zac Minson 2021 年 9 月 20 日
I ended up doing this, but was wondering if there is a more general method in the case of 2^N matrices.
Stephen23
Stephen23 2021 年 9 月 21 日
@Zac Minson: using a cell array is a very general method, which works for any number of matrices.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2021 年 9 月 20 日
編集済み: Matt J 2021 年 9 月 21 日
Do you really need all Z{i} at the same time?
[C{1:N}]=ndgrid([-1,1]);
C=reshape( cat(N+1,C{:}) ,[],N);
Z=zeros(N^2,2^N); %EDITED - removed loops
Z(1:N+1:N^2,:)=C;
Z=reshape(Z, N,N,[])
  3 件のコメント
Matt J
Matt J 2021 年 9 月 20 日
Here's a loop-free version:
N = 4; % Set this value
[C{1:N}]=ndgrid([-1,1]);
C=reshape( cat(N+1,C{:}) ,[],N).';
Z=zeros(N^2,2^N);
Z(1:N+1:N^2,:)=C;
Z=reshape(Z, N,N,[])
Z =
Z(:,:,1) = -1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 -1 Z(:,:,2) = 1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 -1 Z(:,:,3) = -1 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 -1 Z(:,:,4) = 1 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 -1 Z(:,:,5) = -1 0 0 0 0 -1 0 0 0 0 1 0 0 0 0 -1 Z(:,:,6) = 1 0 0 0 0 -1 0 0 0 0 1 0 0 0 0 -1 Z(:,:,7) = -1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 -1 Z(:,:,8) = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 -1 Z(:,:,9) = -1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 1 Z(:,:,10) = 1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 1 Z(:,:,11) = -1 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 1 Z(:,:,12) = 1 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 1 Z(:,:,13) = -1 0 0 0 0 -1 0 0 0 0 1 0 0 0 0 1 Z(:,:,14) = 1 0 0 0 0 -1 0 0 0 0 1 0 0 0 0 1 Z(:,:,15) = -1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 Z(:,:,16) = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Zac Minson
Zac Minson 2021 年 9 月 21 日
Thank you so much. I was really wracking my brain over how to generalize this problem. I appreciate your time.

サインインしてコメントする。

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2021 年 9 月 20 日
a=(dec2bin(15:-1:0)-48)*2-1;
z=zeros(4,4,16);
for k=1:16
z(:,:,k)=diag(a(k,:));
end
  1 件のコメント
the cyclist
the cyclist 2021 年 9 月 20 日
Here is a generalization of this solution:
N = 4; % Set this value
a=(dec2bin((2^N-1):-1:0)-48)*2-1;
z=zeros(N,N,2^N);
for k=1:2^N
z(:,:,k)=diag(a(k,:));
end

サインインしてコメントする。


Jan
Jan 2021 年 9 月 20 日
編集済み: Jan 2021 年 9 月 21 日
z = repmat(eye(4), 1, 1, 16);
z(z==1) = 1 - rem(floor((0:15) ./ [1; 2; 4; 8]), 2) * 2; % Inlined DEC2BIN
Generalized:
n = 3;
z = repmat(eye(n), 1, 1, 2^n);
z(z==1) = 1 - rem(floor((0:2^n-1) ./ pow2(0:n-1).'), 2) * 2;
Now z(:,:,i) is the i.th matrix. This is more efficient than storing them in a cell. But if you want this:
zC = num2cell(z, [1,2]);

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by