problem with assigning in loop

1 view (last 30 days)
Hamid
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

Accepted Answer

Matt J
Matt J on 13 Dec 2014
Get rid of the line
Ep=zeros(u,6);
  2 Comments
Hamid
Hamid on 13 Dec 2014
it works, thank you.
Matt J
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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!