필터 지우기
필터 지우기

problem with assigning in loop

조회 수: 1 (최근 30일)
Hamid
Hamid 2014년 12월 13일
편집: Matt J 2014년 12월 13일
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
Hamid
Hamid 2014년 12월 13일
it works, thank you.
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