Generating Block Matrix Dynamically
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello,
I am generating this matrix in 2d:
0.7071 0.7071
0.7071 0.7071
But for even number (and of course greater than 2) I need to generate that:
0.7071 0.7071 0 0
0.7071 0.7071 0 0
0 0 0.7071 0.7071
0 0 0.7071 0.7071
But I can not handle with my size problem. Here is my code:
function [A,B] = CHSH2d(d)
A=zeros(d,d,2,d);
B=A;
projectors_of_sigma_x = [1/sqrt(2)*[1;1],1/sqrt(2)*[1;-1]];
for k = 1:d
for l =1:d
A(:,:,1,k)=repmat(projectors_of_sigma_x(:,k),1,d);
if d > 2
A(:,:,1,k) = blkdiag(A(:,:,1,k));
end
end
end
end
採用された回答
Ameer Hamza
2020 年 6 月 27 日
編集済み: Ameer Hamza
2020 年 6 月 27 日
This example show how to use blkdiag to create such a matrix
M = [0.7071 0.7071
0.7071 0.7071];
n = 4;
M_cell = repmat({M}, 1, n/2);
M_out = blkdiag(M_cell{:})
Result
M_out =
0.7071 0.7071 0 0
0.7071 0.7071 0 0
0 0 0.7071 0.7071
0 0 0.7071 0.7071
You can avoid for-loop in your code.
14 件のコメント
Gözde Üstün
2020 年 6 月 27 日
Thank you very much. I am trying to implemet this answer to my code
A=zeros(d,d,2,d);
B=A;
projectors_of_sigma_x = [1/sqrt(2)*[1;1],1/sqrt(2)*[1;-1]];
for k = 1:d
A(:,:,1,k)=repmat(projectors_of_sigma_x(:,k),1,2);
if k > 2
A(:,:,1,k) = repmat({A(:,:,1,k)}, 1, d/2);
A(:,:,1,k) = blkdiag(A{:});
end
end
But I have still the same error: Unable to perform assignment because the size of the left side is 4-by-4 and the size of the right side is 2-by-2. I understood the error and why I am getting this error but I dont know how I can solve
By the way I have to use for loop because I will have d dimension.
Ameer Hamza
2020 年 6 月 28 日
Check the following code. It runs without error
d = 4;
A=zeros(d,d,2,d);
B=A;
projectors_of_sigma_x = [1/sqrt(2)*[1;1],1/sqrt(2)*[1;-1]];
for k = 1:d
% A(:,:,1,k)=repmat(projectors_of_sigma_x(:,k),1,2);
if k > 2
temp = repmat({projectors_of_sigma_x}, 1, d/2);
A(:,:,1,k) = blkdiag(temp{:});
end
end
Gözde Üstün
2020 年 6 月 28 日
But how can you decide that you are using the first element of projectors_of_sigma_x which is equal 1/sqrt(2)*[1;1] or you are using second element of projectors_of_sigma_x which is equal 1/sqrt(2)*[1;-1]??
What I mean is that:
A(:,:1,1) should be equal 1/sqrt(2)*[1;1] and
A(:,:,1,2) should be equal 1/sqrt(2)*[1;-1]
In your code I could not see this point because you are using {projectors_of_sigma_x} directly but {projectors_of_sigma_x} has 2 elements
Ameer Hamza
2020 年 6 月 28 日
In question, you mentioned output with the required output for
0.7071 0.7071
0.7071 0.7071
so the required output is not clear. Can you show the output matrix for the case of
0.7071 0.7071
0.7071 -0.7071
Gözde Üstün
2020 年 6 月 28 日
I have the matrix as input either:
0.7071 0.7071
0.7071 0.7071
and in this case my output should be:
0.7071 0.7071 0 0
0.7071 0.7071 0 0
0 0 0.7071 0.7071
0 0 0.7071 0.7071
or I have an input matrix like that:
0.7071 0.7071
-0.7071 -0.7071
And in this case my output matrix should be:
0.7071 0.7071 0 0
-0.7071 -0.7071 0 0
0 0 0.7071 0.7071
0 0 -0.7071 -0.7071
Ameer Hamza
2020 年 6 月 28 日
What is difference between output of these two slices: A(:,:1,1) and A(:,:,1,2) in this case.
Gözde Üstün
2020 年 6 月 28 日
編集済み: Gözde Üstün
2020 年 6 月 29 日
For the A(:,:,1,1) case the output should be:
0.7071 0.7071 0 0
0.7071 0.7071 0 0
0 0 0.7071 0.7071
0 0 0.7071 0.7071
And for the A(:,:,1,2) case the output should be:
0.7071 0.7071 0 0
-0.7071 -0.7071 0 0
0 0 0.7071 0.7071
0 0 -0.7071 -0.7071
This is because first element of sigma_x for A(:,:1,1) and second element of sigma_x for A(:,:,1,2)
projectors_of_sigma_x = [1/sqrt(2)*[1;1],1/sqrt(2)*[1;-1]];
Ameer Hamza
2020 年 6 月 29 日
What about A(:,:1,3) and A(:,:1,4). Similarly, A(:,:2,1), A(:,:2,2), A(:,:2,3), and A(:,:2,4).
Gözde Üstün
2020 年 6 月 29 日
編集済み: Gözde Üstün
2020 年 7 月 1 日
@Ameer Hamza sorry for confusion,
Let's take d = 4
Then A(:,:,1,1) should be
0.7071 0.7071 0.7071 0.7071
0.7071 0.7071 0.7071 0.7071
0.7071 0.7071 0.7071 0.7071
0.7071 0.7071 0.7071 0.7071
A(:,:,1,2)
0.7071 0.7071 0.7071 0.7071
-0.7071 -0.7071 -0.7071 -0.7071
0.7071 0.7071 0.7071 0.7071
-0.7071 0.7071 -0.7071 0.7071
A(:,:,1,3)
0.7071 0.7071 0 0
0.7071 0.7071 0 0
0 0 0.7071 0.7071
0 0 -0.7071 -0.7071
A(:,:,1,4)
0.7071 0.7071 0 0
-0.7071 -0.7071 0 0
0 0 0.7071 0.7071
0 0 0.7071 0.7071
For A(:,:,2,1) and the following I have another values which is called sigma_z [[1;1],[1,-1]]
Sorry for the confusion again
Ameer Hamza
2020 年 7 月 1 日
The pattern is not clear how it is extended from the case of n=2 to this n=4 case. Do you have a general formula in mathematical form to generate such a matrix for any value of n?
Gözde Üstün
2020 年 7 月 1 日
Sorry I cannot give you more details I know that for A(:,:,1,1) in 4d we should see the same matrix in 2d but this time we should see like 4*4 matrix and this is the same for A(:,:1,2) and for A(:,:,1,3) we should see a block diagonal matrix which is include A(:,:,1,1) and A(:,:,1,2) as blocks and for A(:,:,1,4) again we should see the block matrix but this time exchange of blocks of the A(:,:,1,3)
Cant we try anything?
Gözde Üstün
2020 年 7 月 1 日
And if you dont know just tell me how can I produce 4*4 matrix for the fisrt case(forgat about everything for block diagonal) I have the following code and I want to produce 4*4 matrix which each element should be 1/sqrt(2)*[1;1] which is the first elemet of projectors_of_sigma_x
function [A,B] = CHSH2d(d)
A=zeros(d,d,2,d);
B=A;
projectors_of_sigma_x = [1/sqrt(2)*[1;1],1/sqrt(2)*[1;-1]];
for k = 1:d
for l =1:d
A(:,:,1,k)=repmat(projectors_of_sigma_x(:,k),1,d);
if d > 2
A(:,:,1,k) = ....??
end
end
end
end
Ameer Hamza
2020 年 7 月 2 日
See this
d = 4;
A=zeros(d,d,2,d);
B=A;
projectors_of_sigma_x = [1/sqrt(2)*[1;1],1/sqrt(2)*[1;-1]];
for k = 1:d/2 % what is pattern for k>d/2
A(:,:,1,k)=repmat(projectors_of_sigma_x(:,k),d/2,d);
end
It saves the values of A(:,:1,1) and A(:,:1,2). But how to extent the patten beyond that for an arbitrary value of 'd' is not clear to me. The description you provided is clear for d=4, but what if d=8 or d=16.
Gözde Üstün
2020 年 7 月 3 日
Thank you very much I asked you the wrong question so sorry I will ask the right question now in the different page
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
