deleting and adapting columns in matrix of function handles
3 次查看(过去 30 天)
显示 更早的评论
Hello,
If you a matrix that contains function handles, so for example:
A = @(x) [x , x^2, x^3; 2*x, 2*x^2, 2*x^3]
How can I delete/adapt the second column of A ?
0 个评论
回答(1 个)
dpb
2015-11-29
There are no "columns" in A and A is not a matrix...
>> A = @(x) [x , x^2, x^3; 2*x, 2*x^2, 2*x^3]
A =
@(x)[x,x^2,x^3;2*x,2*x^2,2*x^3]
>> whos A
Name Size Bytes Class Attributes
A 1x1 16 function_handle
>> func2str(A)
ans =
@(x)[x,x^2,x^3;2*x,2*x^2,2*x^3]
>> whos ans
Name Size Bytes Class Attributes
ans 1x31 62 char
>>
As shown, A is a single function handle despite it defining a function that returns a 2D matrix when executed.
You either create a new function handle as desired or, in a more roundabout way, convert to a string representation and edit on the string and then redefine the handle from that new string via func2str
You can, of course, create an array of function handles and treat each element within as the above single handle and rewrite individual elements inside such. NB: however, they must be cell arrays.
>> B=[@(x) 1, @(x) x, @(x) x^2]
Error using horzcat
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
>> B={@(x) 1, @(x) x, @(x) x^2}
B =
@(x)1 @(x)x @(x)x^2
>> whos B
Name Size Bytes Class Attributes
B 1x3 228 cell
>>
Also note, now the array is simply a cell array, just so happens the cell content is a set of function handles.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!