Storing value outside the loop using reshape function

조회 수: 1 (최근 30일)
Karanvir singh Sohal
Karanvir singh Sohal 2021년 4월 12일
댓글: Karanvir singh Sohal 2021년 4월 13일
Hello everyone!
I was working on some code and I can't silly I'm doing. Here I have attached my problem in short format. I know its still quite lengthy but please help me out. After hours I'm unable to figure out what I'm doing wrong.
l=[4; 2.8];
[mL,nL]=size(l);
A=[15.0 22.5 30.0 37.5 45.0 52.5 60.0 ]';
for h=1:2
L=l(h);
X=[L*100/16 L*100/10];
XD = A(A>=X(1,1) & A<= X(1,2));
[mX,nX]=size(XD);
index = 0;
for i=1:mX
index=index+1;
LL(index)=l(h);
XDD(index)=XD(i);
end
A1 = [reshape(LL,[],1)];
A2 = [reshape(XDD,[],1)];
L=[A1 A2]
end
Running the above code gives following output:
L = 4 30
4 37.5
L = 2.8 22.5
4 37.5
Desired Output
L = 4 30
4 37.5
L = 2.8 22.5

채택된 답변

Stéphane Vaillant
Stéphane Vaillant 2021년 4월 12일
t seems the variables LL and XDD are uninitialized and so keep their value on the second iteration of the loop. What happens when adding LL=[]; XDD=[]; just under "for h=1:2" ?
  댓글 수: 1
Karanvir singh Sohal
Karanvir singh Sohal 2021년 4월 13일
Thanks @Stéphane Vaillant it work!
But if I have a huge script I have to initialize variables every time and for large number of variables it becomes hard. Is there any alternative for this?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by