problem with assigning in loop

2 次查看(过去 30 天)
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
Matt J 2014-12-13
Get rid of the line
Ep=zeros(u,6);
  2 个评论
Matt J
Matt J 2014-12-13
编辑:Matt J 2014-12-13
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by