Define Matrix array as a function
21 views (last 30 days)
Show older comments
Marzieh Parsa
on 19 Oct 2017
Commented: Walter Roberson
on 21 Oct 2017
Hi everybody.
Would you please guide me how to define an array of a Matrix as a function? For example how can array or components of a Matrix be a Sinc function?
Thanks in advance.
0 Comments
Accepted Answer
Walter Roberson
on 19 Oct 2017
In basic MATLAB, the closest you can get is to have a cell array of function handles that you would have to access and apply to the inputs. (Or, I suppose, an array of some kind of object oriented objects that you created yourself for this purpose.)
In the Symbolic Toolbox, each element of a symbolic array can be used to store a symbol that is a symbolic function. For example,
syms y(t)
A(t) = [sin(t), sinc(t), y]
A(pi/4)
ans =
[ 2^(1/2)/2, (4*sin(pi^2/4))/pi^2, y(pi/4)]
However, this formation is more
A = symfun([sin(t), sinc(t), y], t)
so you will see that A(t) = [ sin(t), sin(pi*t)/(t*pi), y(t)] where sinc(t) has been evaluated already instead of remaining as an unresolved function call.
If you were to instead use
A = [sin(t), sinc(t), y]
then although you would still see A = [ sin(t), sin(pi*t)/(t*pi), y(t)] looking exactly the same content, the result would not be a symbolic function, and attempting A(pi/2) for example would get you a subscript error.
It is not possible to construct a numeric array in which some of the entries are "macros" like in Excel such that changing other entries in the array would change the calculated result at that location.
4 Comments
Walter Roberson
on 21 Oct 2017
You are getting confused by the display of the matrix. MATLAB puts decorations on it. Look at pretty(A) for example.
If all of the entries are to be the same then you could
A(t) = repmat(sinc(t), 5, 5);
However if all of the entries are the same then I do not think this would help you in your Receiver / Transmitter problem.
More Answers (1)
Cam Salzberger
on 19 Oct 2017
Edited: Cam Salzberger
on 19 Oct 2017
Hello Marzieh,
It seems like there may be a relatively fundamental misunderstanding here. Let me see if I can put it clearly.
Numeric arrays: These are simply numbers. These numbers may have meaning that you assign to them, but they don't represent anything by themselves. For example, if you do this:
x = 0:pi/20:2*pi;
ySin = sin(x);
ySinc = sinc(x); % If you have Signal Processing Toolbox
you have now created two numeric arrays. "ySin" doesn't represent that sine function, but it does contain the values gotten by calculating the sine function at each of the points in "x". Similarly for "ySinc".
Symbolic variables and functions: If you are using the Symbolic Math Toolbox, you can create symbolic variables and functions. For example:
syms x
f = sin(x);
Now "f" represents the sine function in a more fundamental way. However, it does not contain any numeric values yet, it is more of an instruction on how to perform the computation. You would compute it with:
subs(f, 0)
to substitute in the value of 0 for "x". You can have arrays of symbolic functions, which will produce array output:
f = [sin(x) cos(x)];
subs(f, pi)
Function handles: Similar to symbolic functions, these are more of instructions on how to perform the computation. However, while the symbolic functions can be used in further symbolic computation, solving, or simplifying, function handles are just instructions on how to run a particular function. That said, you can do something like this:
f = @sin;
Now you have a handle to the numeric sine function in MATLAB. You can compute values like this:
x = 0:pi/20:2*pi;
f(x)
You can have cell arrays of function handles, though it can be confusing trying to evaluate them all at once. Here's a quick example:
f = {@sin @cos};
cellfun(@(h) feval(h, 1), f)
I hope this helps to give you an overview of different "function types" in MATLAB.
-Cam
4 Comments
Steven Lord
on 20 Oct 2017
So let's say that you were able to create a matrix A with "all elements are Sinc Function". How would you then try to use that matrix? Don't try to explain it with code or pseudocode; explain it in words as though we were completely unfamiliar with your application (because we are!)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!