- Extra bracket at the end of the last line.
- In one of the terms, you forgot to index into phi(i), using instead just "phi".
storing a matrix using new variables established by 'eval'
조회 수: 2 (최근 30일)
이전 댓글 표시
I am trying to get a for loop introduce a new variable for every loop using the 'eval' function and store a matrix in the new variable.
elements = 3;
nodes = 2;
phi = [pi / 3,pi-pi/3,0];
for i = 1:elements
eval(['stiffness_' num2str(i) '=zeros(2)'])
eval(['stiffness_' num2str(i) '=[(cos(phi(i)))^2,sin(phi(i)) * cos(phi);sin(phi(i)) * cos(phi(i)),(sin(phi(i)))^2]'])
end
but it's not working. Anyone out there know how to do this?
댓글 수: 0
채택된 답변
the cyclist
2011년 2월 3일
There were two syntax errors:
More importantly, there is a much better way to code this, using cell arrays. (Notice, in my code below, the curly brackets indexing into the variable "stiffness".) Search the documentation for "cell array" for details. You'll be glad you did.
elements = 3;
nodes = 2;
phi = [pi / 3,pi-pi/3,0];
for i = 1:elements
stiffness{i}=zeros(2)
stiffness{i}=[cos(phi(i))^2,sin(phi(i)) * cos(phi(i));sin(phi(i)) * cos(phi(i)),sin(phi(i))^2]'
end
There is also a good explanation here:
추가 답변 (1개)
Jiro Doke
2011년 2월 3일
There are better ways to do this. Take a look at the answers presented in your other post.
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!