Storing and passing all iterations to an array outside the nested for loops

조회 수: 1 (최근 30일)
Hello, my problem is basically that I don't know how to store and pass all 8736 different values from L(k) to the outside of nested for loops in a form of an array (8736x1), so I can use it for further processing and comparing with some other arrays in my code. Code is below...
Lw =[52x1]; Ld=[7x1]; Lh=[24x6]; Ly=number;
for i=1:length(Lw)
for j=1:length(Ld)
for k=1:length(Lh)
if ((i>=1 && i<=8)||(i>=44 && i<=52))
if (j>=1 && j<=5)
L(k)=Lw(i)*Ld(j)*Lh(k,1)*Ly;
else
L(k)=Lw(i)*Ld(j)*Lh(k,2)*Ly;
end
end
if (i>=18 && i<=30)
if (j>=1 && j<=5)
L(k)=Lw(i)*Ld(j)*Lh(k,3)*Ly;
else
L(k)=Lw(i)*Ld(j)*Lh(k,4)*Ly;
end
end
if (i>=9 && i<=17)||(i>=31 && i<=43)
if (j>=1 && j<=5)
L(k)=Lw(i)*Ld(j)*Lh(k,5)*Ly;
else
L(k)=Lw(i)*Ld(j)*Lh(k,6)*Ly;
end
end
L(k)
end
end
end
L to be passed here with all 8736 values
Thank you in advance.

채택된 답변

per isakson
per isakson 2019년 12월 23일
편집: per isakson 2019년 12월 23일
Read Multidimensional Arrays carefully and then try (which implements the advise of Image Analyst)
>> clearvars
>> cssm
>> sum(double(isnan(L(:))))
ans =
0
>> numel(L)
ans =
8736
>>
where the script, cssm, is your script with a few modifications
%% cssm
Lw = rand(52,1); Ld=rand(7,1); Lh=rand(24,6); Ly = 17;
L = nan( length(Lw), length(Ld), length(Lh) ); % pre-allocate
for ii=1:length(Lw)
for jj=1:length(Ld)
for kk=1:length(Lh) % size(Lh,1) (if that's what you intend) is better
if ((ii>=1 && ii<=8)||(ii>=44 && ii<=52))
if (jj>=1 && jj<=5)
L(ii,jj,kk)=Lw(ii)*Ld(jj)*Lh(kk,1)*Ly;
else
L(ii,jj,kk)=Lw(ii)*Ld(jj)*Lh(kk,2)*Ly;
end
end
if (ii>=18 && ii<=30)
if (jj>=1 && jj<=5)
L(ii,jj,kk)=Lw(ii)*Ld(jj)*Lh(kk,3)*Ly;
else
L(ii,jj,kk)=Lw(ii)*Ld(jj)*Lh(kk,4)*Ly;
end
end
if (ii>=9 && ii<=17)||(ii>=31 && ii<=43)
if (jj>=1 && jj<=5)
L(ii,jj,kk)=Lw(ii)*Ld(jj)*Lh(kk,5)*Ly;
else
L(ii,jj,kk)=Lw(ii)*Ld(jj)*Lh(kk,6)*Ly;
end
end
end
end
end
  댓글 수: 2
Image Analyst
Image Analyst 2019년 12월 23일
Banjo, also take note of the nice change he made to change the variable names from i to ii and j to jj because i and j also function as the imaginary variable in MATLAB so we recommend to NOT use i and j as variable names.
Banjo
Banjo 2019년 12월 23일
편집: Banjo 2019년 12월 23일
Thank you both!! I also used reshape at the end, but anyhow I made it to work.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 12월 23일
편집: Image Analyst 2019년 12월 23일
L is available after all the loops terminate. It does not exist solely inside the loop and then vanish when the loops end. Simply pass L to your other code for further processing.
If you don't want to overwrite L on the inner loop, simply add additional indexes L(i, j, k).
  댓글 수: 3
Image Analyst
Image Analyst 2019년 12월 23일
If you don't want to overwrite L on the inner loop, simply add additional indexes L(i, j, k).
Banjo
Banjo 2019년 12월 23일
Sorry but where should I add additional indexes? Thank you for your help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by