for looop convert to arrayfun

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
David Hill el 29 de Abr. de 2021
Please explain what you are trying to do. What are your inputs and expected outputs?
Question Mr
Question Mr el 29 de Abr. de 2021
Here is my original code: (not the whole is just the point)
[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;
output is a matrix
And I tried to create my own function for change two "for" loop
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
Output is matrix like in the original "EF(i,j)" (well that is the plan)
When I called:
EFG = gpuArray(EF);
...
EFG = arrayfun(@gpuefg,X,Y,Z, nElec, 'UniformOutput', false );
It's crashed but I dont know why,
EFG is a gpuArray,
X,Y,Z my function's inputs
nElec is my function's maxiter
So the point is, I want to swap these two "for loops" for one "arryfun"

Iniciar sesión para comentar.

 Respuesta aceptada

Matt J
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

Question Mr
Question Mr el 29 de Abr. de 2021
Thank you
Matt J
Matt J el 29 de Abr. de 2021
Editada: Matt J el 29 de Abr. de 2021
You are welcome, but please Accept-click one of the answers (I expect the first one) to indicate that it resolved your problem.
Question Mr
Question Mr el 30 de Abr. de 2021
I have another question. I tried you solution but I got this error:
"Use of functional workspace is not supported.
For more information see Tips."
What should I do now? I've wroten the code into the .m file
Matt J
Matt J el 30 de Abr. de 2021
Editada: Matt J el 30 de Abr. de 2021
Why did you Accept the answer if it didn't work? Why not just go with the other answer, which I told you should be better than arrayfun?

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 29 de Abr. de 2021
Editada: Matt J el 29 de Abr. de 2021
The more efficient way to do what you are attempting is with the 2 lines of code,
X=X(:); Y=Y(:); Z=Z(:);
EF = 1 - ( (X-X.').^2 + (Y-Y.').^2 + (Z-Z.').^2)/2 ;

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Abr. de 2021

Editada:

el 30 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by