Using the switch statement inside the for loop.

59 views (last 30 days)
Hello everyone, I have a doubt in using the switch statement inside the for loop. The problem is :
I want to write the switch statement inside the for loop, so slover should check the condition of the switch automatically without user input.
For an example:
for i = 1:n
for j = 1:n
switch Contacts
case Rigid_body1_Rigid_body2
case Rigid_body_oil
case Rigid_body_Air
case Oil_Air
So Here I know How to write it by allowing user to enter the condition like below
For an example;
Function a = Condutivity(Contacts)
Instead of allowing the user to define the contact manually, I need slover to decide the condition based on the contact.
Function a = Conductivity
any suggestions are also welcomed and solution will be appreciated.
Thanks in Advance.
Jan on 7 Jun 2019
@surendra kumar Aralapura mariyappa: Sorry, I cannot follow you. This is too much information, which does not concern the actual problem. Neither "oil" nor "rigid body" matters, such that these details are confusing only. On the other hand, important details are unclear:
function L = Conductivity
[A,B,D] = Matrix;
What does the 2nd line mean? What does the function Matrix() return?
Maybe all you want is to replace
switch Contacts(i,j)
This sentence is not clear to me also:
"Here input arguments will also be called which means arguments will be defined in another matrix file and that file will be called in the main function file."
What is a "another matrix file"? Which is the "main function file"?

Sign in to comment.

Answers (2)

Jos (10584)
Jos (10584) on 7 Jun 2019
I suggest you try to avoid a switch statement inside the for-loop as this will probably slow down things a lot. Depending on what you aim for, you can e.g. create a function handle before the loops. Here is a very simple example:
WhatToDo = 'plusone'
switch WhatToDo
case 'plusone'
fn = @(k) k + 1
case 'twice'
fn = @(k) [k k]
fn = @(k) 'otherwise'
for i = 1:10
disp(fn(i)) ;
  1 Comment
surendra kumar Aralapura mariyappa
But my model is like:
Contacts = [ 1,2,1,2,1,1,2,2,1,2,2,1];
Here you can see in the first code of mine, user has to input the condition and slover gives the L each time.
But I don't want user to give the input ;
Contacts = [ 1,2,1,2,1,1,2,2,1,2,2,1];
the condition will be changed for different model. for example
Contacts = [1 2 2 2,1,1,2,1,2,1];
In the above condition, user can easily know what type of contact is it. But I need slover to know by itslef for different types of model.
And also here we can't define the Conditon in the main function file always. If the Matrix file changes according to the contact, slover should adopt itslef to that changes.
Here goes more explantion
Diameter_ RB1 RB2 RB3 Air
D = [ 55 55 55 0];
Contact area between the Rigid bodies and Air
A = [ 0 4749.50 0 2375.80 ;
4749.50 0 4749.50 1835.80;
0 4749.50 0 2375.80 ;
2375.80 1835.80 2375.80 0];
Breadth1 Breadth2 Breadth3 Breadth_Air
B = [ 25 30 20 0];
So now If I chnage the order or if I change the Length of the matrix solver shloud define the contact istelf and give the L.
For example
B = [ 25 0 30 20];
D = [55 0 55 55];
Now user can know, contact is changed and give the condition as contact. But I need slover to know this automatically and find L.
Hope I gave alomost all explanation with an example.

Sign in to comment.

Steven Lord
Steven Lord on 7 Jun 2019
It's still not really clear, but what I think you want is for your function to infer or deduce which type of problem you're trying to solve (Rigid_body1_Rigid_body2, Rigid_body_oil, Rigid_body_Air, Oil_Air, etc.) without the user of your function having to specify that information explicitly. The data parameters that your user enters would somehow inform your function that you have two rigid bodies, a rigid body in an oil bath, etc.
That strikes me as potentially very dangerous. If you later on add a new type of problem that your function should handle it could be indistinguishable from an existing type of problem, breaking existing users of your function. Making your users enter the information about the type of problem to solve, while requiring your users to do a bit more work when solving a new problem, would let them (and you) be certain your function is solving the correct problem. There may be ways to make this safer (passing information around in struct arrays or objects that capture some information about what they represent without the user having to explicitly maintain that data or remember to pass it into the function) but that may be a bit more advanced a maneuver than you might want.
If that's not what you want, then I'm afraid you're going to need to explain a bit more. Start at a high level. Please answer these questions with short (ideally no more than one to three sentence) answers, no code.
  • What problem are you trying to solve or what are you trying to compute? If possible, ELI5.
When I say ELI5 that doesn't actually mean explain it like we're five years old, but as the rules for the subreddit of that name states "Unless OP states otherwise, assume no knowledge beyond a typical secondary education program. Avoid unexplained technical terms. Don't condescend; "like I'm five" is a figure of speech meaning "keep it clear and simple.""
For MATLAB Answers you can assume a bit more knowledge than a secondary education, but don't assume we have as much knowledge of your application or your application area as you do.
  • What do the potential inputs to and the variables (specifically A, B, and D) used in the Conductivity function represent? Remember, no code -- explain in words.
  • What's the pain that you're trying to avoid by allowing your user to call your Conductivity function without the Contacts input argument? How would this benefit users of your function?
  1 Comment
surendra kumar Aralapura mariyappa
@Steven Lord, thank you for the quick response and sorry for the late reply.
I think it will be better if I explain my problem with an example.
I have a model(1) of three different materials in series and air around them, 1 2 3 and 4(air). Now I need to find the value L between each contact which means L between 1 and 2, L between 2 and 3, L between 3 and 4, L between 1 and 4, L between 2 and 4 by using the equations(the called function in the main file). Here materials ocntact has one equation( function fie) LA and material-air contact has one equation(function file) LB. Since it is just a simple model and we know the contacts type, we can write the code in the main function particularly work for this model.
But I need the same code to work for the N number of different contacts (material-material and material-air) and also for changed position, which means if I changed the position of material 2 into air and 4 into material and 5 becomes air and model got extra contacts.
So according to the contact, main function call the equation functions regularly until all the contacts are covered, irrespective of the model. Like some model will have 50 different contacts and othere model will have 200 different contacts in different way.
Like Model1_big_materialscontact = [ 1 3 5 9 4];
Model1_big_material_air_contacts = [ 2 6 7 8 10];
the above two rows show how the model is structured. But this strucutre will not be same for all the models. So sample code should work for this big model as well, without changing any thing in the slover.
all the possible contacts are here:
1 = Material_material
2 = Material_air
3 = material_oil
4 = air_oil
5 = air_air
6 = oil_oil
For the sake of easiness, I am considering oly 1 and 2
Coming to A ,D and B.
D stands for diamter of the materials
D = [ D_1, D_2, D_3, 0] for the unchanged sample model 1
B stands for width of the materials
B = [ B_1, B_2, B_3, 0];
A stands for the contact area b/w materials or material-air
A = [ 0 A_1_2 0 A_1_4;
A_2_1 0 A_2_3 A_2_4;
0 A_3_2 0 A_3_4;
A_4_1 A_4_2 A_4_3 0];
Here A_1_2 and A_2_1 are same likewise for A_1_4 and A_4_1 and 2 3 4....
In the main function file (slover) calls, each of the function file(equation) for the different contacts should be called according to the type of contacts repeatdely.
which means
for i = 1:n
for j = 1:n { may be this idea is worng)
if Model1_big_materialscontact
LA =
LB =
Here explicity user define the type of contacts using switch or if statement.
But I need to know how can I write the code to make the slover to define the contact is 1 and go for LA and contact is 2 go for LB repeatdely for n contacts. User is not allowed to define the contact type explicitly in the slover like using switch or if statement. Why? I want user oly to change the Matrix of D, B , and A in matrix file since it is an input arguments to equation files and user can't do it(definig the type of contacts) for big models like 250 contacts every time.
At the end, solver give the matrix of LA and LB
like LA = [ 5236.255 4562.35 1245.14......];
LB = [ 4581.25 7456.245 7796.245 4785.26...........];
Above thing I can do on my own. But need help in defining the contacts by sover itself.
I got an Idea for it, but not as per I needed, I need help in this also.
In c++, We can build the matrix of array (maps)
A = [A = [ 0 A_1_2 | 1 0 A_1_4 | 2;
A_2_1 |1 0 A_2_3 |1 A_2_4 |2;
0 A_3_2|1 0 A_3_4|2;|
A_4_1 2 A_4_2|2 A_4_3|2 0];
Here user define the contact area along with type of contact between them.
I know it is possible in C++, but How in MATLAB?
I know it is a bit confusing, kindly read from the beginning for the understanding purpose.
any suggestions are most welcomed.
Thanks in Advance.

Sign in to comment.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!