Giving a Different Value to Elements of Array
4 次查看(过去 30 天)
显示 更早的评论
Gözde Üstün
2020-7-8
Hello,
I have a value for A(:,:,3,1) and another value for A(:,:,3,2). Similarly, I have different values for A(:,:,4,1) and A(:,:,4,2) But in this for loop I cant give values to elements.
How can I do that?
for i=3:basis_number
for k =1:2
A(:,:,i,k) = repmat(blkdiag(temp,temp2),d/4,d/4);
A(:,:,i,k) = repmat(blkdiag(temp2,temp),d/4,d/4);
A(:,:,i,k) = repmat(blkdiag(temp3,temp4),d/4,d/4);
A(:,:,i,k) = repmat(blkdiag(temp4,temp3),d/4,d/4);
end
end
3 个评论
Mahesh Taparia
2020-7-13
Hi
Can you elaborate more, what you want to do and what error you are getting?
Gözde Üstün
2020-7-13
Here I want to do that:
A(:,:,3,1) = repmat(blkdiag(temp,temp2),d/4,d/4);
A(:,:,3,2) = repmat(blkdiag(temp2,temp),d/4,d/4);
A(:,:,4,1) = repmat(blkdiag(temp3,temp4),d/4,d/4);
A(:,:,4,2) = repmat(blkdiag(temp4,temp3),d/4,d/4);
But instead of writing by one by, I wanted to use for loop
采纳的回答
Stephen23
2020-7-13
编辑:Stephen23
2020-7-14
Using numbered variables is a sign that you are doing something wrong.
In this case, using indexing would allow you to use a loop, i.e. instead of this
A(:,:,3,1) = repmat(blkdiag(temp1,temp2),d/4,d/4);
A(:,:,3,2) = repmat(blkdiag(temp2,temp1),d/4,d/4);
A(:,:,4,1) = repmat(blkdiag(temp3,temp4),d/4,d/4);
A(:,:,4,2) = repmat(blkdiag(temp4,temp3),d/4,d/4);
you can do something like this (of course you should actually just create the cell array C right from the start using basic indexing and NOT create variables with numbers in the their names):
V = 3:basis_num;
C = {[],[];[],[];temp1,temp2;temp3,temp4}; % size = basis_num * 2
A = preallocate to correct size
for ii = 3:basis_num
x = C{ii,1};
y = C{ii,2};
A(:,:,ii,1) = repmat(blkdiag(x,y),d/4,d/4);
A(:,:,ii,2) = repmat(blkdiag(y,x),d/4,d/4);
end
EDIT: based on the comments follwing this question:
n = d/2;
r = pow2(n);
C = arrayfun(@(~)rand(2,2),1:r/2,'uni',0);
% generate indices:
X = [1+eye(n);4-eye(n)];
X = fliplr([X;2+X]);
% generate output array:
%A = zeros(d,d,r,2); % preallocate!
k = 0;
for ii = 3:basis_number
for jj = 1:2
k = k+1;
A(:,:,ii,jj) = blkdiag(C{X(k,:)});
end
end
13 个评论
Gözde Üstün
2020-7-13
Hello, Thank you very much for your answer it worked :) Can I ask some question?
1)Why did not you use V anywehere?
2)A = preallocate to correct size
what is this sentence I didnt understand anything from this sentence I just close it
3)I wrote code like that:
function [A,B] = trying(d,basis_number)
%A=zeros(d,d,basis_number,d);
%A=zeros(d,d,2,2);
A=zeros(d,d,basis_number,2);
B=A;
...
... Here there are something for basis number smaller than 3
if basis_number>2 && d==4
V = 3:basis_number;
C = {[],[];[],[];temp,temp2;temp3,temp4}; % size = basis_num * 2
C2= {[],[];[],[];temp5,temp6;temp7,temp8};
% A = preallocate to correct size
for ii = 3:basis_number
x = C{ii,1};
y = C{ii,2};
x2 = C2{ii,1};
y2 = C2{ii,2};
A(:,:,ii,1) = repmat(blkdiag(x,y),d/4,d/4);
A(:,:,ii,2) = repmat(blkdiag(y,x),d/4,d/4);
B(:,:,ii,1) = repmat(blkdiag(x2,y2),d/4,d/4);
B(:,:,ii,2) = repmat(blkdiag(y2,x2),d/4,d/4);
end
elseif basis_number>2 && d==6
D = {[],[];[],[];temp,temp2;temp3,temp4}; % size = basis_num * 2
D2= {[],[];[],[];temp5,temp6;temp7,temp8};
for ii = 3:basis_number
x3 = D{ii,1};
y3 = D{ii,2};
x4 = D2{ii,1};
y4 = D2{ii,2};
A(:,:,ii,1) = repmat(blkdiag(x3,y3,x3),d/6,d/6);
A(:,:,ii,2) = repmat(blkdiag(x3,x3,y3),d/6,d/6);
B(:,:,ii,1) = repmat(blkdiag(x4,y4,y4),d/6,d/6);
B(:,:,ii,2) = repmat(blkdiag(y4,x4,x4),d/6,d/6);
end
end
But as you can see it seems too much. And If I have 6 d then the permutations should go till the 8 basis count and this time I should have:
A(:,:,3,1) = repmat(blkdiag(x3,x3,y3),d/6,d/6);
A(:,:,3,2) = repmat(blkdiag(x3,y3,x3),d/6,d/6);
A(:,:,4,1) = repmat(blkdiag(y3,x3,x3),d/6,d/6);
A(:,:,4,2) = repmat(blkdiag(y3,y3,x3),d/6,d/6);
A(:,:,5,1) = repmat(blkdiag(y3,x3,y3),d/6,d/6);
A(:,:,5,2) = repmat(blkdiag(x3,y3,y3),d/6,d/6);
A(:,:,6,1) = repmat(blkdiag(x4,y4,x4),d/6,d/6);
.
.
.
And I cannot go more than 4 basis even if I change the size of C
Best regards
Stephen23
2020-7-13
编辑:Stephen23
2020-7-13
Your questions:
1- V is not used, it was left over from my first example code. You can ignore it.
2- Read this, it explains what preallocation is and how to do it:
3- Although I used x and y in my answer, to create a general solution for any size you would need to use one variable and a loop. Then generate the indices for the permutations that you need, and use a comma-separated list to supply those arrays/values to blkdiag:
Gözde Üstün
2020-7-13
1) I already closed the sentence V thanks
2)ok I understood
3) Sorry but no I didnt understand. My aim is to not to use dimension in if statement but now it is impossible
If I dont use this if
elseif basis_number>2 && d==6
the program cannot understand which dimension I am studying so I added this block
elseif basis_number>2 && d==6
D = {[],[];[],[];temp,temp2;temp3,temp4}; % size = basis_num * 2
D2= {[],[];[],[];temp5,temp6;temp7,temp8};
for ii = 3:basis_number
x3 = D{ii,1};
y3 = D{ii,2};
x4 = D2{ii,1};
y4 = D2{ii,2};
A(:,:,ii,1) = repmat(blkdiag(x3,y3,x3),d/6,d/6);
A(:,:,ii,2) = repmat(blkdiag(x3,x3,y3),d/6,d/6);
B(:,:,ii,1) = repmat(blkdiag(x4,y4,y4),d/6,d/6);
B(:,:,ii,2) = repmat(blkdiag(y4,x4,x4),d/6,d/6);
end
end
Then the code includes too many sentences and repetition. And each time I have to redefine the size of C For instance for 4basis C is eqaul {[],[];[],[];temp,temp2;temp3,temp4} but for 6 basis C is equal {[],[];[],[],[];[],[][];temp,temp2;temp3,temp4}
And for the fifth basis I cant write these sentence A(:,:,ii,1) or A(:,:,ii,2)
Stephen23
2020-7-13
"And each time I have to redefine the size of C For instance for 4basis "
I don't see any reason why you need to redefine C for different d, just generate the appropriate indices on each loop iteration (I have no idea what your required permutations are). Your approach of creating lots of variables and using if restricts you from writing generalized code which expands with d or basis_number.
The generalized solution would be something like this:
C = {all required values/arrays in here};
for ii = 1:whatever
X = permutation indices based on d and whatever else
Y = ditto
A(:,:,ii,1) = blkdiag(C{X});
A(:,:,ii,2) = blkdiag(C{Y});
end
Presumably you know the rules for those permutations that you want to generate.
"And If I have 6 d then the permutations should go till the 8 basis count"
I have no idea what that means.
"My aim is to not to use dimension in if statement but now it is impossible"
I don't see why: if you know the logic then you can define the indices that access the required arrays from C, use a comma-separated list to provide them to blkdiag. There seems to be nothing forcing you to use if.
"And for the fifth basis I cant write these sentence..."
Why not?
I do not have your data, I cannot run your example code,and your explanations are not very clear, so I cannot provide you with a complete, working, tested solution. I have shown you a general approach that you can probably use.
Avoid creating numbered variables, lots of C, D, etc. and using if. They will not help you. Using arrays and indexing will help you.
Gözde Üstün
2020-7-13
Hello, thanks for your answer.
For 4d, code is ok. I have this values:(2^2)
A(:,:,3,1) = repmat(blkdiag(temp1,temp2),d/4,d/4);
A(:,:,3,2) = repmat(blkdiag(temp2,temp1),d/4,d/4);
A(:,:,4,1) = repmat(blkdiag(temp3,temp4),d/4,d/4);
A(:,:,4,2) = repmat(blkdiag(temp4,temp3),d/4,d/4);
But for 6d this time I should these values: (the all triple permutations of temp1 and temp2 and temp3 and temp4 ) Here we have 2^3 basis
A(:,:,3,1) = repmat(blkdiag(temp1,temp2,temp1),d/6,d/6);
A(:,:,3,2) = repmat(blkdiag(temp2,temp1,temp1),d/6,d/6);
A(:,:,4,1) = repmat(blkdiag(temp1,temp1,temp2),d/6,d/6);
A(:,:,4,2) = repmat(blkdiag(temp2,temp2,temp1),d/6,d/6);
A(:,:,5,1) = repmat(blkdiag(temp1,temp2,temp2),d/6,d/6);
A(:,:,5,2) = repmat(blkdiag(temp2,temp1,temp2),d/6,d/6);
A(:,:,6,1) = repmat(blkdiag(temp3,temp4,temp3),d/6,d/6);
A(:,:,6,2) = repmat(blkdiag(temp4,temp3,temp3),d/6,d/6);
A(:,:,7,1) = repmat(blkdiag(temp3,temp3,temp4),d/6,d/6);
A(:,:,7,2) = repmat(blkdiag(temp4,temp4,temp3),d/6,d/6);
A(:,:,8,1) = repmat(blkdiag(temp4,temp3,temp4),d/6,d/6);
A(:,:,8,2) = repmat(blkdiag(temp3,temp4,temp4),d/6,d/6);
And for 8d I should have all fourt permutations of temp with temp2 and temp3 with temp4. Here we have 2^4 basis
I really dont know how it could be possible with your ideas. It will not a smarter code It will be massive And I am still thinking for your code how it could be possible Anyway Thank you very much
Stephen23
2020-7-13
编辑:Stephen23
2020-7-13
Your examples keep changing. In your earlier comment you gave this table as the expected output for d=6:
% 1st example:
A(:,:,3,1) = repmat(blkdiag(x3,x3,y3),d/6,d/6);
A(:,:,3,2) = repmat(blkdiag(x3,y3,x3),d/6,d/6);
A(:,:,4,1) = repmat(blkdiag(y3,x3,x3),d/6,d/6);
A(:,:,4,2) = repmat(blkdiag(y3,y3,x3),d/6,d/6);
A(:,:,5,1) = repmat(blkdiag(y3,x3,y3),d/6,d/6);
A(:,:,5,2) = repmat(blkdiag(x3,y3,y3),d/6,d/6);
A(:,:,6,1) = repmat(blkdiag(x4,y4,x4),d/6,d/6);
...
% 2nd example:
A(:,:,3,1) = repmat(blkdiag(temp1,temp2,temp1),d/6,d/6);
A(:,:,3,2) = repmat(blkdiag(temp2,temp1,temp1),d/6,d/6);
A(:,:,4,1) = repmat(blkdiag(temp1,temp1,temp2),d/6,d/6);
A(:,:,4,2) = repmat(blkdiag(temp2,temp2,temp1),d/6,d/6);
A(:,:,5,1) = repmat(blkdiag(temp1,temp2,temp2),d/6,d/6);
A(:,:,5,2) = repmat(blkdiag(temp2,temp1,temp2),d/6,d/6);
A(:,:,6,1) = repmat(blkdiag(temp3,temp4,temp3),d/6,d/6);
...
The order has changed: if we assume that x3==temp1 and y3==temp2 then here are the first lines of those two examples::
A(:,:,3,1) = repmat(blkdiag(temp1,temp1,temp2),d/6,d/6); % 1st example
A(:,:,3,1) = repmat(blkdiag(temp1,temp2,temp1),d/6,d/6); % 2nd example
They are clearly not the same order. Most/all of the other lines have changed order too.
Why do the examples keep changing order? Is the order random? Are any of them actually correct?
Stephen23
2020-7-13
编辑:Stephen23
2020-7-13
"I really dont know how it could be possible with your ideas"
I really don't know how it would be possible with lots of numbered variables, IFs, etc.
"It will not a smarter code It will be massive..."
I doubt that. Here is a simple working example (unlike your examples, none of which can be run):
% random fake data:
d = 6; % must be even
n = d/2;
r = pow2(n);
C = arrayfun(@(~)rand(2,2),1:r/2,'uni',0);
% generate indices:
X = [1+eye(n);2-eye(n)];
X = fliplr([X;2+X]);
% generate output array:
A = zeros(d,d,r,2); % preallocate!
k = 0;
for ii = 3:r
for jj = 1:2
k = k+1;
A(:,:,ii,jj) = blkdiag(C{X(k,:)});
end
end
Considering that this code works for any d I would not consider it particularly "massive". It is certainly much more versatile than your hard-coded approach using lots of variables and IFs. Here is your 1st example (I removed the superfluous repmat, mapped x3->C{1}, y3->C{2}, etc., corrected the order of A(:,:,6,1) to match the first row, and followed the same pattern for the remaining rows):
B(:,:,3,1) = blkdiag(C{1},C{1},C{2});
B(:,:,3,2) = blkdiag(C{1},C{2},C{1});
B(:,:,4,1) = blkdiag(C{2},C{1},C{1});
B(:,:,4,2) = blkdiag(C{2},C{2},C{1});
B(:,:,5,1) = blkdiag(C{2},C{1},C{2});
B(:,:,5,2) = blkdiag(C{1},C{2},C{2});
B(:,:,6,1) = blkdiag(C{3},C{3},C{4});
B(:,:,6,2) = blkdiag(C{3},C{4},C{3});
B(:,:,7,1) = blkdiag(C{4},C{3},C{3});
B(:,:,7,2) = blkdiag(C{4},C{4},C{3});
B(:,:,8,1) = blkdiag(C{4},C{3},C{4});
B(:,:,8,2) = blkdiag(C{3},C{4},C{4});
Lets compare the outputs:
>> isequal(A,B)
ans = 1
So far everything seems to be working as expected, no "massive" code required as far as I can tell.
Gözde Üstün
2020-7-13
Hello
Regarding my example the orders mean that the permutation each of them has a different block. They are correct For A(:,:,3,1) I want to see that: repmat(blkdiag(temp1,temp1,temp2),d/6,d/6);and for the A(:,:,3,2) I want to see that: repmat(blkdiag(temp2,temp1,temp1),d/6,d/6); This means permutation. Lets do it clear
In two dimension I have that:
alice(:,:,1,1) = %this is the forst block
0.5000 -0.5000
-0.5000 0.5000
alice(:,:,2,1) = %third block
0 0
0 1
alice(:,:,1,2) = %this is the second block
0.5000 0.5000
0.5000 0.5000
alice(:,:,2,2) = %5. block
1 0
0 0
Now In 4d and 2 basis (A(4,4,2,2)) I should see that:
alice(:,:,1,1) =
0.5000 -0.5000 0 0
-0.5000 0.5000 0 0
0 0 0.5000 -0.5000
0 0 -0.5000 0.5000
alice(:,:,2,1) =
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
alice(:,:,1,2) =
0.5000 0.5000 0 0
0.5000 0.5000 0 0
0 0 0.5000 0.5000
0 0 0.5000 0.5000
alice(:,:,2,2) =
1 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
Up to now my cod eis perfectly fine
And now for 4d and 3 basis I should see that: (A(4,4,3,2))
alice(:,:,1,1) =
0.5000 -0.5000 0 0
-0.5000 0.5000 0 0
0 0 0.5000 -0.5000
0 0 -0.5000 0.5000
alice(:,:,2,1) =
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 1
alice(:,:,3,1) = %combination of the first block and second block
0.5000 -0.5000 0 0
-0.5000 0.5000 0 0
0 0 0.5000 0.5000
0 0 0.5000 0.5000
alice(:,:,1,2) =
0.5000 0.5000 0 0
0.5000 0.5000 0 0
0 0 0.5000 0.5000
0 0 0.5000 0.5000
alice(:,:,2,2) =
1 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
alice(:,:3,2) %another permutation of fisrt block and second block
0.5000 0.5000 0 0
0.5000 0.5000 0 0
0 0 0.5000 -0.5000
0 0 -0.5000 0.5000
And if I have A(4,4,4,2) In addition the above result we should see that :
alice(:,:,4,1) = %permutation of the thord and fourth block
0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0
and
alice(:,:,4,2) = %
another permutation of the third and fourth block
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 1
For 6 d this time we have 3 block and their permutations.
I saved this block as temp.
for instance temp =
0.5000 -0.5000
-0.5000 0.5000
temp2 =
0.5000 0.5000
0.5000 0.5000
temp3 =
0 0
0 1
temp4 =
1 0
0 0
By the way thank you very much for your code
And that is why I dont know what should I write inside C?
Should C be
C={temp,temp2,temp3} ????
Fun array is a very different structure How can I use it in our case
Gözde Üstün
2020-7-13
Ok I wrote C= {temp,temp2,temp3,temp4}
it is good but I could not use temp3 and temp4. I can see just the permutation of temp and temp2 I should also see permutation of temp3 and temp4
Gözde Üstün
2020-7-13
No when I try to define
C= {temp,temp2,temp3,temp4} it can not for 6 d
so I should write it with arrayfun but this time I had error:
"the elemnt of funarray should be intenger"
Gözde Üstün
2020-7-13
I have this code:
n = d/2;
r = pow2(n);
C= {temp,temp2,temp3,temp4}
C = arrayfun(@(~)rand(2,2),1:r/2,'uni',0);
% C2={temp5,temp6,temp7,temp8}
% generate indices:
X = [1+eye(n);4-eye(n)];
X = fliplr([X;4+X]);
% generate output array:
%A = zeros(d,d,r,2); % preallocate!
k = 0;
for ii = 3:r
for jj = 1:2
k = k+1;
A(:,:,ii,jj) = blkdiag(C{X(k,:)});
% B(:,:,ii,jj) = blkdiag(C{X(k,:)});
end
end
And when try for d I have index error:
Index exceeds the number of array elements (4).
Error in newCHSH (line 58)
A(:,:,ii,jj) = blkdiag(C{X(k,:)});
What is problem I am really tired of this code :((
Gözde Üstün
2020-7-13
Finally I have that:
n = d/2;
r = pow2(n);
C= {temp,temp2,temp3,temp4}
C2={temp5,temp6,temp7,temp8}
% C = arrayfun(@(~)rand(2,2),1:r/2,'uni',0);
% generate indices:
X = [1+eye(n);4-eye(n)];
X = fliplr([X;2+X]);
% generate output array:
%A = zeros(d,d,r,2); % preallocate!
k = 0;
for ii = 3:basis_number
for jj = 1:2
k = k+1;
A(:,:,ii,jj) = blkdiag(C{X(k,:)});
B(:,:,ii,jj) = blkdiag(C2{X(k,:)});
end
end
and it is working
Thank you very much !
Gözde Üstün
2020-7-13
and if you write your last comment as an asnwer, I will accpet that as a correct answer :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)