Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simplify Matlab for/if statements further

  1. Feb 25, 2015 #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 (Text):
      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
     
  2. jcsd
  3. Feb 25, 2015 #2

    Mark44

    Staff: Mentor

    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.

     
  4. Feb 26, 2015 #3

    DrClaude

    User Avatar

    Staff: Mentor

    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 (Text):

    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.
     
  5. Mar 16, 2015 #4
    Hi DrClaude,

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

    Thanks
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Simplify Matlab for/if statements further
  1. Matlab if statements (Replies: 1)

Loading...