# for looop convert to arrayfun

4 views (last 30 days)

Show older comments

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?

### Accepted Answer

Matt J
on 29 Apr 2021

Edited: Matt J
on 29 Apr 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 Comments

Matt J
on 30 Apr 2021

### See Also

### Categories

### Products

### Community Treasure Hunt

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

Start Hunting!