Inverse input/output of switch case function

6 views (last 30 days)
Say I have the following function:
function [y] = my_func(x)
switch x
case 'name a'
y = 'word i';
case 'name b'
y = 'word j';
case 'name z'
y = 'word k';
otherwise
error('error')
end
I would like to avoid to create a new function, and thus use this function(my_func) such that if I give the input 'word j' I get the output 'name b', or if I give the input 'word k' I get the output 'name z'.
Is there a way?
Thank you in advance for any hint.
  3 Comments
VBBV
VBBV on 15 Jun 2023
Moved: VBBV on 15 Jun 2023
Intechange the places for characters inside switch-case statement
Stephen23
Stephen23 on 15 Jun 2023
Edited: Stephen23 on 15 Jun 2023
If this is required in both directions, perhaps simple text arrays and indexing would suffice.
Then you could avoid duplicating any data.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 15 Jun 2023
Edited: John D'Errico on 15 Jun 2023
In general, no, that is not possible, at least to get MATLAB to do this automatically for you (if your mapping is not a single-valued relationship.) In fact, that is provably impossible for some cases. In some other extreme cases where a switch is not involved, it might be computationally intractable.
Consider the case of myfunc1, changed slightly from yours.
function [y] = my_func1(x)
switch x
case 'name a'
y = 'word i';
case 'name b'
y = 'word k';
case 'name z'
y = 'word k';
otherwise
error('error')
end
There, two inputs yield the same output. And that means one can never know which input resulted in that duplicated output.
In the simple case of your function, where the result is what a mathematician would call a bijective function,
yes, you COULD do something. Essentially you would pass in every possible input, then recording the outputs. Now use a dictionary to remember those results. A dictionary in MATLAB is a tool that can take any set of inputs, and relate them to a set of outputs.
We can try this for my_func. Below, I've copied your original, bijective function. Then, I'll run it for each possible input, and create a dictionary.
Df = dictionary;
Df('name a') = my_func('name a');
Df('name b') = my_func('name b');
Df('name z') = my_func('name z');
At this point, we have a dictionary with three entries.
Df
Df =
dictionary (stringstring) with 3 entries: "name a" ⟼ "word i" "name b" ⟼ "word j" "name z" ⟼ "word k"
As you can see, it embodies the mapping that is in my_func, going forwards. But you want the reverse mapping. So really, I built the dictionary the wrong way. (Yes, I could use the iskey function in MATLAB to tell it to search through the Df dictionary.) Next, I'll just build a dictionary that embodies the reverse mapping.
Dr = dictionary;
Dr(my_func('name a')) = 'name a';
Dr(my_func('name b')) = 'name b';
Dr(my_func('name z')) = 'name z';
Dr
Dr =
dictionary (stringstring) with 3 entries: "word i" ⟼ "name a" "word j" ⟼ "name b" "word k" ⟼ "name z"
Now, we can use these dictionaries. As you can see, Dr does give the reverse lookup you want.
Dr('word i')
ans = "name a"
Or, I might have built a single dictionary that had all the words and names in the one dictionary, combining the two.
D = dictionary([Df.keys;Dr.keys],[Df.values;Dr.values])
D =
dictionary (stringstring) with 6 entries: "name a" ⟼ "word i" "name b" ⟼ "word j" "name z" ⟼ "word k" "word i" ⟼ "name a" "word j" ⟼ "name b" "word k" ⟼ "name z"
The dictionary D will perform the mapping in either direction.
D('name b')
ans = "word j"
D('word k')
ans = "name z"
And, of course, when it cannot find the requested key, it fails.
D('somethin else')
Error using ()
Key not found.
function [y] = my_func(x)
switch x
case 'name a'
y = 'word i';
case 'name b'
y = 'word j';
case 'name z'
y = 'word k';
otherwise
error('error')
end
end
Is this equivalent to writing a new function? In a sense, it probably is. You are forcing MATLAB to store every possible output, and relate each output to the original input. Could you as easily have just written the reverse mapping as a function? YES!

More Answers (1)

Dyuman Joshi
Dyuman Joshi on 15 Jun 2023
Add extra cases
function [y] = my_func(x)
switch x
case 'name a'
y = 'word i';
case 'name b'
y = 'word j';
case 'name z'
y = 'word k';
case 'word i'
y = 'name a';
case 'word j'
y = 'name b';
case 'word k'
y = 'name c';
otherwise
error('error')
end
  1 Comment
Rub Ron
Rub Ron on 15 Jun 2023
Thanks, this is a good alternative. But for my particular case, it works better to create a new function.

Sign in to comment.

Categories

Find more on Characters and Strings 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!