필터 지우기
필터 지우기

Inverse input/output of switch case function

조회 수: 1 (최근 30일)
Rub Ron
Rub Ron 2023년 6월 15일
이동: VBBV 2023년 6월 15일
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
VBBV
VBBV 2023년 6월 15일
이동: VBBV 2023년 6월 15일
Intechange the places for characters inside switch-case statement
Stephen23
Stephen23 2023년 6월 15일
편집: Stephen23 2023년 6월 15일
If this is required in both directions, perhaps simple text arrays and indexing would suffice.
Then you could avoid duplicating any data.

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2023년 6월 15일
편집: John D'Errico 2023년 6월 15일
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!
  댓글 수: 1
Rub Ron
Rub Ron 2023년 6월 15일
Thanks, it seems more sensible to create a new function.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 6월 15일
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
Rub Ron
Rub Ron 2023년 6월 15일
Thanks, this is a good alternative. But for my particular case, it works better to create a new function.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by