forming a function handles in matrix
Mostra commenti meno recenti
I want to form a set of function handles in a row matrix.
i wrote script like below.
w = 2;
Nr = 20
nr = @(phi)zeros(1, Nr);
wr = @(phi)zeros(1, Nr);
for n = 1:Nr
for i = 1:w
Awr = 50*(cos(i*2)-cos(i*9));
nr(1, n) = @(phi)nr(1, n)+Awr*cos(i*1.7);
end
wr(1, n) = @(phi)nr(1, n)
end
Iam getting the below error
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
how to rectify this??
Risposte (1)
Steven Lord
il 4 Ott 2021
0 voti
MATLAB used to allow nonscalar arrays of function handles, but that functionality was removed probably 10 to 15 years ago. As the error message suggested, store your function handles in a cell array instead.
7 Commenti
Bathala Teja
il 4 Ott 2021
f = {@sin, @cos, @(t) t.^2}
y = f{3}(1:10) % returns the first 10 perfect squares
fh = f{2} % returns the function handle @cos
Bathala Teja
il 5 Ott 2021
Creating an array of function handles is not allowed. It will not work.
Creating a cell array containing function handles is allowed.
f = cell(1, 3);
for k = 1:3
f{k} = @(x) x.^k + k*x;
end
% Use the elements of f by indexing then passing the input
y = f{2}(1:10) % Evaluate the function handle corresponding to k = 2 at x = 1:10
check = (1:10).^2 + 2*(1:10) % Manual check, will be the same as y
Bathala Teja
il 6 Ott 2021
@Bathala Teja: function handles cannot be multiplied (or have any numeric operation applied to them). But you can certainly evaluate the function handles (just as Steven Lord showed) and multiply their outputs:
f{1}(3)*f{2}(3)
% ^^^ ^^^ Evaluate!
Categorie
Scopri di più su Data Type Identification in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!