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 个评论
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.
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
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
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
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
What is difference between output of these two slices: A(:,:1,1) and A(:,:,1,2) in this case.
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]];
What about A(:,:1,3) and A(:,:1,4). Similarly, A(:,:2,1), A(:,:2,2), A(:,:2,3), and A(:,:2,4).
@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
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?
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?
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
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.
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!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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)
