Storing value outside the loop using reshape function

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Stéphane Vaillant
Stéphane Vaillant il 12 Apr 2021
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 Commento
Karanvir singh Sohal
Karanvir singh Sohal il 13 Apr 2021
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?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by