矩阵的方向导数 [英] Directional Derivatives of a Matrix
问题描述
我的工作区中有40个结构.我需要编写一个脚本来计算所有元素的方向导数.这是代码:
I have 40 structures in my Workspace. I Need to write a script to calculate the directional derivatives of all the elements. Here is the code :
[dx,dy] = gradient(Structure_element_1.value);
dxlb = min(min(dx));
dxub = max(max(dx));
dylb = min(min(dy));
dyub = max(max(dy));
[ddx,ddy] = gradient(gradient(Structure_element_1.value));
ddxlb = min(min(ddx));
ddxub = max(max(ddx));
ddylb = min(min(ddy));
ddyub = max(max(ddy));
这是一个元素的代码.我需要找出所有40个元素相同的元素,然后再使用.任何人都可以帮忙.
This is the code for one element. I Need to find out the same for all the 40 elements and then use it later. Can anyone help with this.
推荐答案
要回答您的字面问题,您应该将变量存储在单元格数组.如果所有结构都具有相同的字段,则可以通过索引单个数组变量(例如Structure_element
:
To answer your literal question, you should store the variables in a structure array or at least a cell array. If all of your structures have the same fields, you can access all of them by indexing a single array variable, say Structure_element
:
for i = 1:numel(Structure_element)
field = Structure_element(i).value
% compute gradients of field
end
现在要解决实际梯度计算的问题. gradient
函数计算,其中是您的数据矩阵.通常,MATLAB函数会知道请求了多少个输出参数.调用gradient(gradient(F))
时,内部gradient
调用的 first 输出上将调用外部gradient
.这意味着您当前正在获得.
Now to address the issue of the actual gradient computation. The gradient
function computes an approximation for , where is your matrix of data. Normally, a MATLAB function is aware of how many output arguments are requested. When you call gradient(gradient(F))
, the outer gradient
is called on the first output of the inner gradient
call. This means that you are currently getting an approximation for .
我怀疑您确实是在尝试获取.为此,您必须从内部调用gradient
中获取两个输出,并将它们分别传递给
外部调用,然后选择正确的输出:
I suspect that you are really trying to get . To do this, you have to get both outputs from the inner call to gradient
, pass them separately to the
outer call, and choose the correct output:
[dx,dy] = gradient(F);
[ddx, ~] = gradient(dx);
[~, ddy] = gradient(dy);
请注意分开的通话.引入了波浪号作为<junk
或类似名称的实际变量即可.
Note the separated calls. The tilde was introduced as a way to ignore function arguments in MATLAB Release 2009b. If you have an older version, just use an actual variable named junk
or something like that.
这篇关于矩阵的方向导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!