Giving a Different Value to Elements of Array

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

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by