insert value in vector using for loop

조회 수: 4 (최근 30일)
Raphael
Raphael 2012년 10월 22일
hey!
i want to insert an integer value in an existing vector using a for/while loop, so that the ascending order is preserved. so far i have programmed several functions, however i can't establish one that works with a for/while loop. here is the the version i am working with right now:
function insert=insertdata(vec,int)
indi=vec>=int;
ind=find(indi,1);
if ind~=0
newvec=[vec(1:ind-1), int, vec(ind:end)];
else
newvec=[vec, int];
end
insert=newvec;
thanks for your help!
  댓글 수: 1
Image Analyst
Image Analyst 2012년 10월 22일
Why do you want a version that works using a loop??? And what ascending order? Is vec sorted prior to passing into the function?

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2012년 10월 22일
find() returns the empty array when nothing is found, rather than returning 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