# Error in loop: left and right sides have a different number of elements

1 Ansicht (letzte 30 Tage)
Miriam am 27 Okt. 2022
Kommentiert: Miriam am 27 Okt. 2022
I wanna make a loop out of the following lines:
dt=1;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs1=real(roots(p))*D
dt=2;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs2=real(roots(p))*D
dt=3;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs3=real(roots(p))*D
dt=4;
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs4=real(roots(p))*D
For every Zvs i get for solutions (Zvs (4,1)). I tried like this:
dt = [1:1:4];
Zvs = NaN(length(dt),4);
for i=1:length(dt)
a0= 0.01*sqrt(d/D)*Fr/D*u*dt;
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p=[a4 a3 a2 a1 a0];
Zvs (i)=real(roots(p))*D
end
I get this Error:
Unable to perform assignment because the left and right sides have a
different number of elements.
Error in Skript (line 26)
Zvs (i)=real(roots(p))*D
I know that the problem ist the different size from Zvs (i), its 1x1 and not 1x4. But i don't know how to fix it.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

RAJA SEKHAR BATTU am 27 Okt. 2022
Bearbeitet: RAJA SEKHAR BATTU am 27 Okt. 2022
You can pre assign the variables to empty or zeros before loop to save the values efficiently. I will make some modifications please check if it works.
a0, p and Zvs variables are changing every iteration
dt = [1:1:4];
Zvs = zeros(4,length(dt));
a0 = zeros(1,length(dt));
p = zeros(length(dt),5);
for i=1:length(dt)
a0(i)= 0.01*sqrt(d/D)*Fr/D*u*dt(i);
a1=0;
a2=7.07;
a3=4.45/tan(phi);
a4=0.786/(tan(phi)*tan(phi));
p(i,:)=[a4 a3 a2 a1 a0(i)];
Zvs(:,i)=real(roots(p(i,:)))*D;
end
##### 4 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
RAJA SEKHAR BATTU am 27 Okt. 2022
The matrices of pre allocation should match the matrices inside the loop.
Please check the size of the matrices. This is the point of pre allocation.
Miriam am 27 Okt. 2022
I got it, now it works. Thanks!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by