Using the switch statement inside the for loop.
59 views (last 30 days)
surendra kumar Aralapura mariyappa on 7 Jun 2019
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
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.
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'
fn = @(k) k + 1
fn = @(k) [k k]
fn = @(k) 'otherwise'
for i = 1:10
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?