# problem with assigning in loop

2 views (last 30 days)
Hamid on 13 Dec 2014
Edited: Matt J on 13 Dec 2014
Hi everybody, I want to assign every minrow to Ep but Ican't and only last row gets number and other rows gets zero.
What should I do?
this is my code; Thank you guys.
u=0;
for i=1:ne
Le(i)=sqrt((Ex(i,1)-Ex(i,2))^2+(Ey(i,1)-Ey(i,2))^2+(Ez(i,1)-Ez(i,2))^2);
if Le(i)~=0
u=u+1;
[ minval , minidx ] = min(available(available(:, 6) > Le(i)/20000, 6));
minrow = available(minidx, :);
Ep=zeros(u,6);
Ep(u,:)=minrow;
end
end

Matt J on 13 Dec 2014
Get rid of the line
Ep=zeros(u,6);
##### 2 CommentsShow 1 older commentHide 1 older comment
Matt J on 13 Dec 2014
Edited: Matt J on 13 Dec 2014
The code is still less than ideal, though. Why not compute Le with vectorized methods in advance of the loop so you can figure out how to pre-allocate Ep properly?
Le=sqrt((Ex(:,1)-Ex(:,2))^2+(Ey(:,1)-Ey(:,2))^2+(Ez(:,1)-Ez(:,2))^2);
Lenz=nonzeros(Le(1:ne));
Ni=length(Lenz);
Ep=zeros(Ni,6);
for i=1:Ni
[ minval , minidx ] = min(available(available(:, 6) > Lenz(i)/20000, 6));
minrow = available(minidx, :);
Ep(i,:)=minrow;
end