using the vector as a condition in the switch statement
Show older comments
Hey, I need to know how to pass row vector or column vector as a condition to a switch statement.
I have two row vectors;
function [M,A] = my_vectors
M = [ 1 3 5 9 8 11 12 13 15 17 20 25 29 31.......];
A = [ 2 4 7 6 10 14 16 19......... ];
end
I need my slover like below:
function my_switch
[M,A] = my_vectors;
for i = 1:n
switch true
case M
statement;
case A
statement
end
end
Here I know we can use multiple conditions in switch statement like below;
Switch true
case { 1, 3, 5, 9, 8}
statement;
case { 2, 4, 6, 7}
statement;
end
But I need to use a n vecotr as a condition in the switch statement since row vector function is being called in the slover(main function).
Here I don't need to enter the number each time to check the number isnumber(b,a) whether the number is there in the vector M or A to exceute the statement like below.
switch true
case ismember(b,M)
statement;
case ismember(b,A)
statement;
end
if there is any wrong in my code plz write the comment section along with the answer for my problem.
any suggestion are most welcomed and thanks in advance.
9 Comments
Walter Roberson
on 9 Jun 2019
switch i
Adam Danz
on 9 Jun 2019
If I understand you correctly, you want a switch-case that looks for and accepts entire vectors. Is that correct? If so, here's one way to do that.
% The vector
A = [2 4 7 6];
% all of the "cases"
cases = {[1 2 3 4 5]; [2 4 6 8]; [9 6 3]; [2 4 7 6]; [1 2 4 7 6]; [4 2 6 7]; [0 0 0 0]};
% determine which case was matched (if any)
isEqIdx = find(cellfun(@(x)isequal(x,A),cases));
if isempty(isEqIdx)
% If there are no matches, this will result in the "otherwise" case match.
isEqIdx = NaN;
end
% use the index as a switch
switch isEqIdx
case 1
case 2
case 3
case 4
case 5
case 6
case 7
otherwise
error('No matches found in switch-case.')
end
surendra kumar Aralapura mariyappa
on 9 Jun 2019
Edited: surendra kumar Aralapura mariyappa
on 9 Jun 2019
"switch true" isn't the correct syntax for switch-cases (please see that link). If the switch statement is merely "true", it will always result in the first case being matched.
Please make it clear what you're trying to pass into the swtich and what cases you require. It sounds like you're trying to match an entire vector which is what my proposal does.
surendra kumar Aralapura mariyappa
on 9 Jun 2019
Edited: surendra kumar Aralapura mariyappa
on 9 Jun 2019
Walter Roberson
on 9 Jun 2019
You are not testing i anywhere in your switch
If you did test i, then it would be a scalar and so would not be the same as the value of the vector A or the vector M.
Your code appears to me to want to test whether i is any of the values in A, or any of the values in M. In which case, switch i
Adam Danz
on 9 Jun 2019
When you share code (which is usually helpful) please format it using the format button.
There are too many mysteries/errors in your code to understand what you're trying to do. For example, what is my_vectors? Where do M and A come from in your my_switch function? What is 'n' and where does that come from? Almost every line of code either has an error or its values come out of thin air.
Rather than explaining the code, I'd like to hear a bigger picture explanation of what you're trying to do.
surendra kumar Aralapura mariyappa
on 9 Jun 2019
surendra kumar Aralapura mariyappa
on 9 Jun 2019
Edited: surendra kumar Aralapura mariyappa
on 9 Jun 2019
Accepted Answer
More Answers (0)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!