I want to create a 12x6 Matrix from six 12x1 eigenvectors; what's wrong with this code?
조회 수: 2 (최근 30일)
이전 댓글 표시
When I execute this code, instead of an intended 12x6 matrix "u" made from u(1), u(2), u(3), etc., I end up with a single 12x1 vector "u", as u gets overwritten each pass.
for i = 1:12;
o = omega(i);
M = [(-o^2*m(1)+k(1)+k(2)), -k(2), 0, 0, 0, 0; -k(2), (-o^2*m(2)+k(2)+k(3)), -k(3), 0, 0, 0; 0, -k(3), (-o^2*m(3)+k(3)+k(4)), -k(4), 0, 0; 0, 0, -k(4), (-o^2*m(4)+k(4)+k(5)), -k(5), 0;0, 0, 0, -k(5), (-o^2*m(5)+k(5)+k(6)), -k(6); 0, 0, 0, 0, -k(6), (-o^2*m(6)+k(6)+k(7))];
u(i)= eig(M);
end
where omega is a 12x1 vector.
What's wrong with the code?
댓글 수: 0
채택된 답변
Andrew Newell
2014년 4월 25일
편집: Andrew Newell
2014년 4월 25일
I'm not sure how you avoided getting a dimension mismatch error there. The thing you're missing is a colon:
u = zeros(length(m),length(omega));
for i = 1:length(omega);
o = omega(i);
M = [(-o^2*m(1)+k(1)+k(2)), -k(2), 0, 0, 0, 0; ...
-k(2), (-o^2*m(2)+k(2)+k(3)), -k(3), 0, 0, 0; ...
0, -k(3), (-o^2*m(3)+k(3)+k(4)), -k(4), 0, 0; ...
0, 0, -k(4), (-o^2*m(4)+k(4)+k(5)), -k(5), 0; ...
0, 0, 0, -k(5), (-o^2*m(5)+k(5)+k(6)), -k(6); ...
0, 0, 0, 0, -k(6), (-o^2*m(6)+k(6)+k(7))];
u(:,i)= eig(M);
end
Note that I have included a few good programming practices: initializing omega, using length(omega) and length(m) in place of the meaningless numbers 12 and 6, and formatting the code so it is easy to understand.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!