Simplify Matlab for/if statements further

In summary, the conversation is about simplifying a loop code and automating certain numbers in a matrix and vector. The code involves using the "subs" function and setting up syntax for the constants. The conversation also includes suggestions for improving the code, such as using a loop for the first index and setting up variables before the loop.
  • #1
bugatti79
794
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
 
Physics news on Phys.org
  • #2
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
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
Hi DrClaude,

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

Thanks
 

1. How can I simplify my Matlab for/if statements further?

One way to simplify for/if statements in Matlab is to use vectorization, which allows you to perform operations on entire arrays rather than individual elements. This can reduce the number of for/if statements needed in your code.

2. Can I use logical indexing to simplify my Matlab for/if statements?

Yes, logical indexing is another technique that can simplify for/if statements in Matlab. It allows you to select elements from an array based on a logical condition, eliminating the need for explicit for/if statements.

3. Are there any built-in functions in Matlab that can simplify for/if statements?

Yes, there are several built-in functions in Matlab that can help simplify for/if statements. These include find, any, all, and isequal, among others.

4. How can I use conditional statements to simplify my Matlab code?

Conditional statements, such as if/else and switch/case can be used to simplify Matlab code by reducing the number of for loops and if statements needed. These statements allow you to execute different sections of code based on specific conditions.

5. Is there a limit to how much I can simplify my Matlab for/if statements?

While it is possible to simplify for/if statements in Matlab to some extent, there is a limit to how much you can simplify them. It is important to balance simplicity with readability and performance in your code. Additionally, some complex problems may require more intricate for/if statements in order to be solved accurately.

Similar threads

Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • General Engineering
Replies
1
Views
2K
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
5
Views
588
  • Advanced Physics Homework Help
Replies
1
Views
4K
Back
Top