# Simplify Matlab for/if statements further

• MATLAB
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

Mark44
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.

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

DrClaude
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:
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.

Hi DrClaude,

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

Thanks