Simplify Matlab for/if statements further

  • MATLAB
  • Thread starter bugatti79
  • Start date
  • #1
719
1
Hi Folks,

This loop code is working fine but I would like to simplify it further. Can anyone help?

Basically, i need syntax to automate the numbers highlighted in bold.

k is a 3*3 matrix, SE_ans is a 3*1 vector, A is a 3*3 identify matrix. The rests are just constants.

Code:
  for i=1:size(k,1)
 
k([B]1[/B],i)=subs(SE_ans([B]1[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])
k([B]1[/B],i)=subs(SE_ans([B]1[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])
k([B]1[/B],i)=subs(SE_ans([B]1[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])

k([B]2[/B],i)=subs(SE_ans([B]2[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])
k([B]2[/B],i)=subs(SE_ans([B]2[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])
k([B]2[/B],i)=subs(SE_ans([B]2[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])

k([B]3[/B],i)=subs(SE_ans([B]3[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])
k([B]3[/B],i)=subs(SE_ans([B]3[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])
k([B]3[/B],i)=subs(SE_ans([B]3[/B],:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,[B]1[/B]), A(i,[B]2[/B]), A(i,[B]3[/B]),1,2,3])
  end

Thanks
 

Answers and Replies

  • #2
34,945
6,705
The [ code ] tags are preventing the bold tags from being rendered correctly. Normally it's a good thing to put your code inside code tags, but not in this case.

for i=1:size(k,1)

k(1,i)=subs(SE_ans(1,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])
k(1,i)=subs(SE_ans(1,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])
k(1,i)=subs(SE_ans(1,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])

k(2,i)=subs(SE_ans(2,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])
k(2,i)=subs(SE_ans(2,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])
k(2,i)=subs(SE_ans(2,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])

k(3,i)=subs(SE_ans(3,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])
k(3,i)=subs(SE_ans(3,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])
k(3,i)=subs(SE_ans(3,:),[theta_1, theta_2, theta_3, k_t1,k_t2,k_t3],[A(i,1), A(i,2), A(i,3),1,2,3])
end
 
  • #3
DrClaude
Mentor
7,599
3,988
Why do you call subs 3 times with the same arguments? Also, why not add a loop for the first index of k? I would also set
Code:
tk = [theta_1, theta_2, theta_3, k_t1,k_t2,k_t3];
AA = [A(i,1), A(i,2), A(i,3),1,2,3];
before the loop, and use that inside the loop.
 
  • #4
719
1
Hi DrClaude,

yes, you are right. I reduced it down to one line and use a double if loop. Works fine now

Thanks
 

Related Threads on Simplify Matlab for/if statements further

  • Last Post
Replies
1
Views
3K
Replies
2
Views
6K
Replies
6
Views
7K
Replies
2
Views
3K
  • Last Post
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
4K
Replies
5
Views
3K
Replies
1
Views
5K
Replies
2
Views
2K
Top