Left and right sides have a different number of elements
12 次查看(过去 30 天)
显示 更早的评论
n = 2
ang = 0 (however I'd like it to iterate throuhg 0 and 90 (I gave a quick shot at ang = [0,90] but no prevail))
U1 = 59.579
U2 = 64.881
U3 = 14.355
U4 = 17.072
My U values print out correctly. However, when I run the code:
for i=1:1:n
Qb11(i)=U1+U2*cosd(2*ang)+U3*cosd(4*ang)
Qb12(i)=U4-U3*cosd(ang*4)
Qb22(i)=U1-U2*cosd(ang*2)+U3*cosd(ang*4)
Qb16(i)=U2/2*sind(ang*2)+U3*sind(ang*4)
Qb26(i)=U2/2*sind(ang*2)-U3*sind(ang*4)
Qb66(i)=1/2*(U1-U4)-U3*cosd(ang*4)
i
Qb(i)=[Qb11(i) Qb12(i) Qb16(i); Qb12(i) Qb22(i) Qb26(i); Qb16(i) Qb26(i) Q66(i)]
end
My matrices fails to compile and I get the error "Unable to preform assignment because the left and right sides have a different number of elements". Any advice?
0 个评论
回答(1 个)
Image Analyst
2019-12-10
You need to preallocate the 3-by-3-by-n array Qb. This works:
n = 2
ang = 0 % (however I'd like it to iterate throuhg 0 and 90 (I gave a quick shot at ang = [0,90] but no prevail))
U1 = 59.579
U2 = 64.881
U3 = 14.355
U4 = 17.072
% My U values print out correctly. However, when I run the code:
% Preallocate a 3-by-3-by-n array.
Qb = zeros(3, 3, n);
for k=1:1:n
Qb11(k)=U1+U2*cosd(2*ang)+U3*cosd(4*ang)
Qb12(k)=U4-U3*cosd(ang*4)
Qb22(k)=U1-U2*cosd(ang*2)+U3*cosd(ang*4)
Qb16(k)=U2/2*sind(ang*2)+U3*sind(ang*4)
Qb26(k)=U2/2*sind(ang*2)-U3*sind(ang*4)
Qb66(k)=1/2*(U1-U4)-U3*cosd(ang*4)
fprintf('Iteration #%d\n', k);
% Load up this slice of the 3-D array Qb.
Qb(:, :, k)=[Qb11(k) Qb12(k) Qb16(k); Qb12(k) Qb22(k) Qb26(k); Qb16(k) Qb26(k) Qb66(k)]
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Big Data Processing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!