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

조회 수: 2 (최근 30일)
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.

채택된 답변

RAJA SEKHAR BATTU
RAJA SEKHAR BATTU 2022년 10월 27일
편집: RAJA SEKHAR BATTU 2022년 10월 27일
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
RAJA SEKHAR BATTU
RAJA SEKHAR BATTU 2022년 10월 27일
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.
Please check and modify your code accordingly.
Miriam
Miriam 2022년 10월 27일
I got it, now it works. Thanks!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by