フィルターのクリア

Giving a Different Value to Elements of Array

2 ビュー (過去 30 日間)
Gözde Üstün
Gözde Üstün 2020 年 7 月 8 日
編集済み: Stephen23 2020 年 7 月 14 日
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 Center および File ExchangeString についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by