104 views (last 30 days)

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.

Walter Roberson
on 9 Jun 2019

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

Opportunities for recent engineering grads.

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

Start Hunting!
## 9 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713070

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713070

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713077

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713077

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713081

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713081

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713086

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713086

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713088

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713088

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713090

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713090

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713091

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713091

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713094

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713094

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713095

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/466214-using-the-vector-as-a-condition-in-the-switch-statement#comment_713095

Sign in to comment.