Can we define a function inside for loop? So if we can define a function whose input is i and outputs are j and k, then for i=1, the outputs will be j=0, k=0, for i=2 the outputs will be j=1, k=0, for i=3, j=1,k=1, for i=4, j=2,k=0,...,for i=7, j=2,k=3 and so on. So in each loop we will get j and k for the corresponding i before defining the main sequence function.
Defining sequence of functions in MATLAB
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Saurabh Madankar
am 11 Feb. 2022
Kommentiert: Saurabh Madankar
am 11 Feb. 2022
Hello,
The Haar mother wavelet is defined as-
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891220/image.png)
Now we can define a sequence of functions
-
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891225/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891230/image.png)
where
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891395/image.png)
So for i=1, j=0 and k=0, i=2, j=1,k=0, i=3, j=1, k=1 and so on and we will define the functions
accordingly. Now I could define these functions individually. But if I need to define till
then how do I define them using for loop or any other way so I don't have to define each function individually?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891240/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/891245/image.png)
1 Kommentar
Akzeptierte Antwort
Walter Roberson
am 11 Feb. 2022
Well, you can do
syms h(t) [1 5]
whos
but although that creates abstract functions with those names, it does not in itself give you a way to define the functions.
syms() does give a way to define symbols as existing without assigning to them, but when you use syms() that way, you cannot give them expressions. sym() (without an s) cannot be used to define variables without assignining to them, and symfun() cannot be used to define variables without assigning to them either. And the "assigning to them" is the problem, as the name to be assigned to is changing.
It isn't that it can't be done... but it is seldom a good idea to use the dirty parts of MATLAB that allow defining variables with constructed names.
I would recommend that you instead use cell arrays.
h{i}(t) = expression involving h{i-1}
Note that even if you did use one of the techniques for creating dynamic variable names, each of the functions is going to end up fully expanding all earlier functions, rather than just referring to the functions. For example,
f(t) = t + 1
g(t) = f^2 + 2
Notice that g(t) does not refer to f(t) -- it evaluated f(t) to get t+1, and squared that result. This is inherent in the way that Symbolic Toolbox works.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Discrete Multiresolution Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!