# How can I run two loops at a time?

2 views (last 30 days)
Wiqas Ahmad on 6 Sep 2021
Answered: Star Strider on 6 Sep 2021
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?

Jan on 6 Sep 2021
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 on 6 Sep 2021
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.
.