for looop convert to arrayfun
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Question Mr
el 29 de Abr. de 2021
I want to convert these for loops into an arrayfun. Always give me an error
"All of the input arguments must be of the same size and shape.
Previous inputs had size 31 in dimension 1. Input #5 has size 1"
[X,Y,Z] = sph2cart(ThetaRad,PhiRad,1.0);
nElec = length(M.lab);
EF(nElec,nElec) = 0;
for i = 1:nElec;
for j = 1:nElec;
EF(i,j) = 1 - ( ( (X(i) - X(j))^2 + ...
(Y(i) - Y(j))^2 + (Z(i) - Z(j))^2 ) / 2 );
end;
end;
I tried to create a new funciton:
function EF = gpuefg(X,Y,Z, maxiter)
EF(maxiter,maxiter) = 0;
for i = 1:maxiter;
for j = 1:maxiter;
EF(i,j) = 1 - ( ( (X(i) - X(j))^2 + ...
(Y(i) - Y(j))^2 + (Z(i) - Z(j))^2 ) / 2 );
end;
end;
end
And call it
EFG = gpuArray(EF);
.
.
.
EFG = arrayfun(@gpuefg,X,Y,Z, nElec, 'UniformOutput', false );
But not working, how should I do this?
2 comentarios
David Hill
el 29 de Abr. de 2021
Please explain what you are trying to do. What are your inputs and expected outputs?
Respuesta aceptada
Matt J
el 29 de Abr. de 2021
Editada: Matt J
el 29 de Abr. de 2021
The way to do this with arrayfun, (which I don't think will be optimal here) would be,
[I,J]=ndgrid(gpuArray(1:nElec));
fun=@(i,j) 1 - ( ( (X(i) - X(j))^2 + (Y(i) - Y(j))^2 + (Z(i) - Z(j))^2 ) / 2 );
EF= arrayfun(fun,I,J);
4 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!