cellfun with gradient [x,y]

1 回表示 (過去 30 日間)
Spyros Polychronopoulos
Spyros Polychronopoulos 2017 年 12 月 4 日
I have a structure array q(1:50,1).p(1:50,1:50). And I was taking the gradient:
for k1=1:100
[part_x(k1).p,part_y(k1).p]= gradient(q(k1).p(:,:)); %gradient
end
I was getting part_x(1:50,1).p(1:50,1:50) and part_y(1:50,1).p(1:50,1:50)
Now I have functions (z) so I created the same structure but with cells: q{1:50,1}.p{1:50,1:50}, and I am trying to do the same but it doesn't work
for k1=1:100
[part_x{k1}.p,part_y{k1}.p]=@(z) (cellfun(@gradient,q{k1}.p{:,:}(z)));
end
Any ideas? I tried arrayfun as well but not working either.
Here is a code example to clarify things:
for k1=1:10
q1(k1).p=rand(10,10);
end
for k1=1:10
[part_x1(k1).p,part_y1(k1).p]= gradient(q1(k1).p); %gradient
end
% Now with function handle the same as above
A=rand(10^3,1);
for k2=1:10^3
a{k2}=@(z) z*A(k2);
end
%preparing the array for the gradient
l=1;
for k3=1:10
for k4=1:10
for k5=1:10
q2(k3).p{k4,k5}=@(z) a{l}(z);
l=l+1;
end
end
end
clear k1 k2 k3 k4 k5 l
% now I want to do what I have done with the matrix above but I have
% functions
%this returns 1 value so doesn't work
for k1=1:10
[part_x2{k1}.p,part_y2{k1}.p]=@(z) (cellfun(@gradient,q2(k1).p(z)));
end
%this doesn't work either
for k1=1:10^3
[part_x2{k1}.p,part_y2{k1}.p]=arrayfun(@(z) gradient(q2(k1).p(z)));
end
  10 件のコメント
Spyros Polychronopoulos
Spyros Polychronopoulos 2017 年 12 月 7 日
thank you Guillaume! you are right just noticed!
Spyros Polychronopoulos
Spyros Polychronopoulos 2017 年 12 月 15 日
similar problem, thank you:
https://uk.mathworks.com/matlabcentral/answers/373142-gradient-of-a-cell-array-with-optimizer

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by