insert value in vector using for loop
4 ビュー (過去 30 日間)
古いコメントを表示
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
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
2012 年 10 月 22 日
find() returns the empty array when nothing is found, rather than returning 0.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!