How can I create a matrix with arrayfun
Show older comments
i tried to swap the 2 FOR loops for arrayfun but i don't understand why it never wants to succeed.
TR = (2 * pi * M.theta) / 360;
PR = (2 * pi * M.phi) / 360;
[X,Y,Z] = sph2cart(TR,PR,1.0);
max = length(M.lab);
M(max,max) = 0;
MG = gpuArray(M);
for i = 1:max;
for j = 1:max;
M(i,j) = 1 - ( ( (X(i) - X(j))^2 + ...
(Y(i) - Y(j))^2 + (Z(i) - Z(j))^2 ) / 2 );
end;
end;
I have tried to implement these two for loops another function like this:
function M = gpuefg(X,Y,Z, maxiter)
M(maxiter,maxiter) = 0;
for i = 1:maxiter;
for j = 1:maxiter;
M(i,j) = 1 - ( ( (X(i) - X(j))^2 + ...
(Y(i) - Y(j))^2 + (Z(i) - Z(j))^2 ) / 2 );
end;
end;
end
and when I called its not worked. Wrong size.
I dont understand how can I swap these to for loops only for an arrayfun
Or how can I "create" a matrix with use arrayfun and use numerical calculations in the arrayfun?
I tried several ways but nothing
CG = arrayfun(@(X,Y,Z,max) gpuefg(X,Y,Z,max) );
CG = arrayfun(@gpuefg, M);
CG = arrayfun(@(X,Y,Z,max) gpuefg(X,Y,Z,max), M);
CG = arrayfun( @(X,Y,Z,max) gpuefg(M, X,Y,Z,max), X,Y,Z,max, 'UniformOutput', false );
Accepted Answer
More Answers (1)
Question Mr
on 1 May 2021
0 votes
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!