For loop through a cell array

3 ビュー (過去 30 日間)
Burak
Burak 2021 年 5 月 21 日
コメント済み: Jan 2021 年 5 月 21 日
Hi, I need to calculate a for loop through a cell array. Each element has 5x5 matrixes. As seen in the code, I need to calculate a for loop in each element of D.
clc
clear all
N=5;
xn=linspace(2,4,N);
yn=xn;
for nx=1:N
for ny=1:N
E1(nx,ny)=xn(nx)*1i+(xn(nx)-yn(ny)^2);
E2(nx,ny)=(xn(nx)+yn(ny))*1i+xn(nx);
end
end
a11=E1.*conj(E1);
a12=E1.*conj(E2);
a21=E2.*conj(E1);
a22=E2.*conj(E2);
A=[a11 a12;a21 a22];
B={E1;E2};
C={conj(E1);conj(E2)};
D=cell(2,2);
for i=1:2
for j=1:2
D{i,j}=B{i}.*C{j};
end
end
  3 件のコメント
Burak
Burak 2021 年 5 月 21 日
Sir, as I mentioned at the top, I need to calculate a for loop for each element of D cell matrix. I don't know how to do it. This is an example code. There are E1,E2,...,E8. and each of them are 150x150 size. So my D cell matrix is 8x8 and each element is 150x150 matrix.
Jan
Jan 2021 年 5 月 21 日
Your code seems to be fine for this job. So which problem do you want to solve now? In the code you use a {2 x 2} cell. If your real data are {8 x 8} simply change the code to:
D = cell(28, 8);
for i = 1:8
for j = 1:8
D{i,j} = B{i} .* C{j};
end
end

サインインしてコメントする。

回答 (1 件)

Jonas
Jonas 2021 年 5 月 21 日
if you want to do a specific operation on each cell, then have a look into the cellfun() function
  1 件のコメント
Jan
Jan 2021 年 5 月 21 日
cellfun is nice and compact, but harder to code, more difficult to debug and it has a larger run time.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by