Automatic detection of relations between workspace variables through functions
I'm trying to write a function what detect this relation between the variables I have got in the workspace:
v1 - fft(v2) = 0
Where v1, v2 are variables of my workspace.
Sometimes I need to know which variables have a certain numerical relation. If I have thirty, I don´t want to be looking for开发者_StackOverflow this relation in "manual way", just introducing a sentence for each pair of different variables.
I would like a function in which I introduce (or I modify this function every time I need it) the sentence (for instance what I wrote before) and the function show me the pair of variables a I am looking for. Does anyone know how to do it?
You can use who() to programatically obtain a list of variables that currently exist. You can then use eval() to get their values. At that point, you can use a fairly trivial nested loop to iterate over all possible pairs, looking for that relationship.
Note 1: Using eval()
for "normal" programming is considered bad style; it should only really be used for meta-programming tasks like this.
Note 2: If you have N
variables in the workspace, there are N^2
ordered pairs. This may take a while to iterate over if N
is large.
Note 3: You're essentially looking for equality between variables, which may not be particularly reliable in floating-point.
精彩评论