using the vector as a condition in the switch statement

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

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
@Adam Danz, Thank you for the quick response.
In your code, again you did what I knew, I mean like this,
cases = {[1 2 3 4 5]; [2 4 6 8]; [9 6 3];........};
But I need to pass the vector A direclty as a condition like this A(:)
switch true
case A(:) % wrong synatx may be
statement
case M(:)
statement
end
I don't need to do any thing in the switch function file(solver), only I hvae to change the condition in the matrix file I mean vectors. Automatically those changed vecotrs should be conditions in the slover as I explained early.
In my question, I included for loop which means a particluar statement will be executed many times
for an example :
M = [ 1 3 5 9 8 11 12 13 15 17 20 25 29 31];
A = [ 2 4 7 6 10 14 16 19 ];
%Please note Vectors M and N will be n numbers like
%M = [ 1 3 5 9 8 11 12 13 15 17 20 25 29 31................];
for i = 1:n
switch true
case A(:)
statement1
case M(:)
statement2
otherwise
disp('not a number')
end
end
Statement1 will be executed 8 times whenever the value of i is same as the value of vector A. Same for the statement1 and its condition M.
Please read my question once again, if you still didn't understand I will explain it deeply.
Thanks
"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.
Okay, Sorry I forgot to mention one more thing here.
function [M,A]= my_matrix
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
function my_switch
[M,A] = my_vectors;
Contacts = {M,A}; %%cell array.
for i = 1:n
switch Contacts
case Contacts(M(:)) %may be wrong synatx
statement1;
case Contacts(A(:)) % may be wrong synatx
statement2;
otherwise
disp('error')
end
end
end
I think now it will be very clear.
Plz ask if you still didn't understand and read all my commnets once again. I need the solution as any cost.
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
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.
I understood now. I will take your idea oly now
for i = 1:n
switch i
case i == A(:)
statement1;
case i == M(:)
statement2
otherwise
statement3
end
end
is this correct now?
function my_idea
A = [ 1 2 5 7 ];
n = 8;
M = [ 3 4 6 9];
for i= 1:n
switch i
case i == A(:)
disp('suri');
case i == M(:)
disp('kumar')
otherwise
disp('error')
end
end
end
still it not working when I tested i also. Kindly help in that at least!

Sign in to comment.

 Accepted Answer

function my_idea
A = [ 1 2 5 7 ];
n = 8;
M = [ 3 4 6 9];
for i= 1:n
switch i
case num2cell(A)
disp('suri');
case num2cell(M)
disp('kumar')
otherwise
disp('error')
end
end
end

More Answers (0)

Categories

Products

Release

R2014a

Community Treasure Hunt

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

Start Hunting!