Inverse input/output of switch case function
    7 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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 commentaires
Réponse acceptée
  John D'Errico
      
      
 le 15 Juin 2023
        
      Modifié(e) : John D'Errico
      
      
 le 15 Juin 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
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
Now, we can use these dictionaries. As you can see, Dr does give the reverse lookup you want.
Dr('word i')
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])
The dictionary D will perform the mapping in either direction.
D('name b')
D('word k')
And, of course, when it cannot find the requested key, it fails.
D('somethin else')
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!
Plus de réponses (1)
  Dyuman Joshi
      
      
 le 15 Juin 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
Voir également
Catégories
				En savoir plus sur Data Type Identification dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




