How can I run two loops at a time?

조회 수: 2 (최근 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.
.

카테고리

Help CenterFile Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by