Giving a Different Value to Elements of Array

2 views (last 30 days)
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 Comments
Mahesh Taparia
Mahesh Taparia on 13 Jul 2020
Hi
Can you elaborate more, what you want to do and what error you are getting?
Gözde Üstün
Gözde Üstün on 13 Jul 2020
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

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 13 Jul 2020
Edited: Stephen23 on 14 Jul 2020
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 Comments
Gözde Üstün
Gözde Üstün on 13 Jul 2020
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 on 13 Jul 2020
and if you write your last comment as an asnwer, I will accpet that as a correct answer :)

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!