Storing value outside the loop using reshape function

1 次查看(过去 30 天)
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
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