How can I run two loops at a time?

1 ビュー (過去 30 日間)
Wiqas Ahmad
Wiqas Ahmad 2021 年 9 月 6 日
回答済み: Star Strider 2021 年 9 月 6 日
for i = 1:length(Reff)
for j = 1:length(EC)
ILP= getsignal([num2str(EC(j)),'\','1100','\',num2str(Reff(i)),'um\out_resultsG_I0.dat']);
QLP= getsignal([num2str(EC(j)),'\','1100','\',num2str(Reff(i)),'um\out_resultsG_Q0.dat']);
I1(:,j)= smooth(sum(ILP,2));
Q1(:,j)= smooth(sum(QLP,2));
......
I want to include both the i,j indices in the I1 and Q1 syntaxes so that they run for both of the loops. By writing
ILP(i,j)= getsignal([num2str(EC(j)),'\','1100','\',num2str(Reff(i)),'um\out_resultsG_I0.dat']);
QLP(i,j)= getsignal([num2str(EC(j)),'\','1100','\',num2str(Reff(i)),'um\out_resultsG_Q0.dat']);
I1(i,j)= smooth(sum(ILP,2));
Q1(i,j)= smooth(sum(QLP,2));
I have the following error:
How can I solve it?

回答 (2 件)

Jan
Jan 2021 年 9 月 6 日
The error message means, that I1(i,j) is a scalar, but smooth(sum(ILP,2)) is not. You cannot assign an array to a scalar.
Maybe you want I1 to be a cell array? Then:
I1 = cell(length(Reff), length(IC));
...
I1{i,j} = smooth(sum(ILP,2));

Star Strider
Star Strider 2021 年 9 月 6 日
Apparently, that occurs when ‘ILP’ and ‘QLP’ become matrices rather than scalars, so when either ‘i’ or ‘j’ is greater than 1.
One option with in the loop would be to use cell arrays:
I1{i,j} = smooth(sum(ILP,2));
Q1{i,j} = smooth(sum(QLP,2));
and then sort the results out later.
With only the code provided, this is the only approach I can think of to solve it.
.

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by