Using the switch statement inside the for loop.
조회 수: 31 (최근 30일)
이전 댓글 표시
surendra kumar Aralapura mariyappa
2019년 6월 7일
댓글: surendra kumar Aralapura mariyappa
2019년 6월 8일
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
statement
case Rigid_body_oil
statement
case Rigid_body_Air
statement
case Oil_Air
Statement;
end
end
end
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.
Like
Function a = Conductivity
any suggestions are also welcomed and solution will be appreciated.
Thanks in Advance.
댓글 수: 3
Jan
2019년 6월 7일
@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)
by
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"?
답변 (2개)
Jos (10584)
2019년 6월 7일
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]
otherwise
fn = @(k) 'otherwise'
end
for i = 1:10
disp(fn(i)) ;
end
Steven Lord
2019년 6월 7일
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?
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!