"Subscripted assignment dimension mismatch in for loop
이전 댓글 표시
c0 = linspace (0.001,2,10);
c1 = linspace(5,30,10);
ac = linspace(0.1,0.5,10);
mc = linspace(0.2,1,10);
xx =[ 0.0037 0.1144 0.2251 0.3358 0.4465 0.5572 0.6679 0.7786 0.8893 1.0000]
for i = 1:length(c0)
for j = 1:length(c1)
for l = 1:length(ac)
for o = 1:length(mc)
c(i,j,l,o) = c0(i).*(1-xx) + c1(j).*(sin(pi*xx)+ac(l).*sin(2*pi*xx)).^mc(o) ;
c = c(i,j,l,o)
end
end
end
end
When I try to run this, I only get this error:
"Subscripted assignment dimension mismatch.
Error in test (line 15)
c(i,j,l,o) = c0(i).*(1-xx) + c1(j).*(sin(pi*xx)+ac(l).*sin(2*pi*xx)).^mc(o) ;"
Why is that? To eliminate different causes I've made the variables (c0,c1,ac and mc) and the xx-vector the same length (but this is not needed). Also, if I try to run without xx, it works (but is obviously not the correct answer). So, its probably the xx vector thats the problem. But why?
Thanks in advance. Åse K.
답변 (1개)
dpb
2016년 4월 5일
Because since xx is a vector, the RHS is a vector of length(xx) with the individual terms modified by the specific constants referred to by the loop indices. In Matlab, a constant multiplies all elements in a vector and so the calculation is successful.
BUT the rub comes in that the LHS is a single subscript location in an array, c and you can't put a vector in a single location.
Now, we don't know what the calculation result is intended to be for each element of c, your problem definition has to be the authority there, but the error comes from the above mismatch.
Are you perhaps missing a subscript on xx for each term so xx should be a single value for each loop iteration or should the values perhaps be summed over all for each or what? If there's supposed to be a vector result then you'll have to store it in a 5D array as
c(i,j,l,o,:) = c0(i).*...
or somesuch.
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!