Unable to perform assignment because the left and right sides have a different number of elements.

1 Ansicht (letzte 30 Tage)
Hello Everyone,
I have a little problem. I want to change a matrix dynamically with values from the For loop and for each iteration solve the system of equations using the backslash operator. Without loop the system of equations can be solved.
Unfortunately, I get as fehelermeldung that the dimensions of the left side do not correspond to the right side.
I would be happy if someone can help me further
Code:
global nquer
global v
global t
cf=15;
Kf=50;
cb=5;
Kb=20;
lambda=4;
mf=0.25;
mb1=0.2;
mb2=0.125;
b=0.1;
t=1;
v=1;
nquer=t*cf*mf;
for nf=1:Kf+1
for nb1=1:cb+1
for nb2=1:(Kb-nb1+1)
if nf-1 < cf && (nb1-1)+(nb2-1) <cb
R(nf,nb1,nb2)=[-(lambda+nf*mf+nb2-1*mb2+nb1*mb1) b*(nf+1)*mf*Indi(nb2) (1-b)*(nf-1+1)*mf (nb2+1)*mb2 (nb1+1)*mb1 lambda*Indi(nf);
0 -b*(nf+1)*mf*Indi(nb2) 0 0 0 0 ;
lambda 0 -(1-b)*(nf+1)*mf 0 0 0,
mb2*nb2 0 0 -(nb2+1)*mb2 0 0;
nb1*mb1 0 0 0 -(nb1+1)*mb1 0;
nf*mf 0 0 0 0 -lambda;
1 1 1 1 1 1];
B1=[0;0;0;0;0;0;1];
x(nf,nb1,nb2)=R\B1
else if cf <nf-1 && (nb1-1)+(nb2-1)<cb
R(nf,nb1,nb2)= [-(lambda*Indi(Kf-nf)+min(nf,cf)*mf+max(nf-cf,0)*v+min(nb2,cb-nb1)*mb2+nb1*mb1) b*cf*mf*Indi(Kf-nf)*Indi(nb2) ((nf+1-cf)*v+(1-b)*cf*mf)*Indi(Kf-nf) (nb2+1)*mb2 (nb1+1)*mb1 ((1-Pnt(nf-1-cf))*lambda) Pnt(nf-cf)*lambda*Indi(Kf-nf)*Indi(nb1);
0 -(b*cf*mf*Indi(Kf-nf)*Indi(nb2)) 0 0 0 0 0;
lambda*Indi(Kf-nf) 0 -(((nf+1-cf)*v+(1-b)*cf*mf)*Indi(Kf-nf)) 0 0 0 0;
mb2*nb2 0 0 -((nb2+1)*mb2) 0 0 0;
mb1*nb1 0 0 0 -((nb1+1)*mb1) 0 0;
(nf-cf)*v 0 0 0 0 -((1-Pnt(nf-1-cf))*lambda) 0;
(cf*mf) 0 0 0 0 0 -(Pnt(nf-cf)*lambda*Indi(Kf-nf)*Indi(nb1));
1 1 1 1 1 1 1];
B2=[0;0;0;0;0;0;0;1];
x(nf,nb1,nb2)=R\B2;
else
R(nf,nb1,nb2)=[-(lambda*Indi(Kf-nf)+15*mf+(nf-cf)*v+2*mb2+mb1*nb1) b*(min(nf+1,cf)*mf)*Indi(Kf-nf)*Indi(nb2) (max(nf+1-cf,0)*v+(1-b)*min(nf+1,cf)*mf)*Indi(Kf-nf) min(nb2+1,cb-nb1)*mb2*Indi(Kb-nb1-nb2) (nb1+1)*mb1*Indi(Kb-nb1-nb2)*Indi(cb-nb1) lambda*Indi(nf) Pnt(nf-cf)*lambda*(1-Indi(nb1+nb2-cb))*Indi(nb1)*Indi(Kf-nf);
0 -(b*min(nf+1,cf)*mf*Indi(Kf-nf)*Indi(nb2)) 0 0 0 0 0;
lambda*Indi(Kf-nf) 0 -((max(nf+1-cf,0)*v+(1-b)*min(nf+1,cf)*mf)*Indi(Kf-nf)) 0 0 0 0;
min(nb2,cb-nb1)*mb2 0 0 -(min(nb2+1,cb-nb1)*mb2*Indi(Kb-nb1-nb2)) 0 0 0;
nb1*mb1 0 0 0 -((nb1+1)*mb1*Indi(Kb-nb1-nb2)*Indi(cb-nb1)) 0 0;
max(nf-cf,0)*v 0 0 0 0 -(lambda*Indi(nf)) 0;
min(nf,cf)*mf 0 0 0 0 0 -(Pnt(nf-cf)*lambda*(1-Indi(nb1+nb2-cb))*Indi(nb1)*Indi(Kf-nf));
B3=[0;0;0;0;0;0;0;1];
x(nf,nb1,nb2))=R\B3;
end
end
end
end
end
"Pnt and Indi "are two own functions that I implemented

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 8 Mai 2021
R(nf,nb1,nb2)=[-(lambda+nf*mf+nb2-1*mb2+nb1*mb1) b*(nf+1)*mf*Indi(nb2) (1-b)*(nf-1+1)*mf (nb2+1)*mb2 (nb1+1)*mb1 lambda*Indi(nf);
0 -b*(nf+1)*mf*Indi(nb2) 0 0 0 0 ;
lambda 0 -(1-b)*(nf+1)*mf 0 0 0,
mb2*nb2 0 0 -(nb2+1)*mb2 0 0;
nb1*mb1 0 0 0 -(nb1+1)*mb1 0;
nf*mf 0 0 0 0 -lambda;
1 1 1 1 1 1];
The right hand side is a 7 x 6 array. The left hand side designates a scalar location. Are you wanting to overwrite all of R or only one particular element of it? Do you need to record all of the R that are generated or do you only need the results of the computation?
B1=[0;0;0;0;0;0;1];
x(nf,nb1,nb2)=R\B1
Were you changing only one element of R two statements ago, or were you generating a complete R that had to be recorded? Because if it was a complete R that had to be recorded then you need to put the indexes version here.
B is 7 x 1 so you should expect a 7x1 result from the \ operator, but you are trying to store the results into a scalar.
You should consider using multidimensional arrays or cell arrays.
  2 Kommentare
Marvin Friedrich
Marvin Friedrich am 8 Mai 2021
I only want to overwrite the "nf", "nb1" and "nb2" elemnts in several differrent Ranges like for "nf" from 0 to 50 I only need the results of the R depending on the Values of "nf" "nb1" and "nb2"
I wasn't shure if i should do several indexes for the different Regions or not. The first Region is only 7x6 where the 2nd and 3rd are 8x7 are.
Yes i want the results from x. Then i will try it without a scalar.
Thank you very much. I will look up multidimensional arrays or cell arrays. You helped me a lot!
Walter Roberson
Walter Roberson am 8 Mai 2021
global nquer
global v
global t
cf=15;
Kf=50;
cb=5;
Kb=20;
lambda=4;
mf=0.25;
mb1=0.2;
mb2=0.125;
b=0.1;
t=1;
v=1;
nquer=t*cf*mf;
for nf=1:Kf+1
for nb1=1:cb+1
for nb2=1:(Kb-nb1+1)
if nf-1 < cf && (nb1-1)+(nb2-1) <cb
R=[-(lambda+nf*mf+nb2-1*mb2+nb1*mb1) b*(nf+1)*mf*Indi(nb2) (1-b)*(nf-1+1)*mf (nb2+1)*mb2 (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ambda*Indi(nf) Pnt(nf-cf)*lambda*(1-Indi(nb1+nb2-cb))*Indi(nb1)*Indi(Kf-nf);
0 -(b*min(nf+1,cf)*mf*Indi(Kf-nf)*Indi(nb2)) 0 0 0 0 0;
lambda*Indi(Kf-nf) 0 -((max(nf+1-cf,0)*v+(1-b)*min(nf+1,cf)*mf)*Indi(Kf-nf)) 0 0 0 0;
min(nb2,cb-nb1)*mb2 0 0 -(min(nb2+1,cb-nb1)*mb2*Indi(Kb-nb1-nb2)) 0 0 0;
nb1*mb1 0 0 0 -((nb1+1)*mb1*Indi(Kb-nb1-nb2)*Indi(cb-nb1)) 0 0;
max(nf-cf,0)*v 0 0 0 0 -(lambda*Indi(nf)) 0;
min(nf,cf)*mf 0 0 0 0 0 -(Pnt(nf-cf)*lambda*(1-Indi(nb1+nb2-cb))*Indi(nb1)*Indi(Kf-nf));
B3=[0;0;0;0;0;0;0;1];
x(nf,nb1,nb2,:)=R\B3;
end
end
end
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by