Giving a Different Value to Elements of Array

3 visualizaciones (últimos 30 días)
Gözde Üstün
Gözde Üstün el 8 de Jul. de 2020
Editada: Stephen23 el 14 de Jul. de 2020
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 comentarios
Mahesh Taparia
Mahesh Taparia el 13 de Jul. de 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 el 13 de Jul. de 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

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by