Fill a vector whose size is defined during run time
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have to create an vector whose size is not mentioned. The user enters element by element, and he decides when to stop.
I wrote down the code in C++, but I don't know how to write it on Matlab.
answer=1;
size=0;
vector<double>v;
while(answer ==1)
{
cout<<"Enter element number "<<size+1<<": "<<endl;
cin>>temp;
v.push_back(temp);
size=size+1;
cout<<"Do you want to add another number? enter 1 for yes: "
cin>>answer;
}
Can i get some help please.
0 个评论
采纳的回答
Guillaume
2015-2-14
编辑:Guillaume
2015-2-14
4 个评论
Guillaume
2015-2-14
temp = input(sprintf('Enter element number %d: ', sz+1));
Note that you shouldn't be using the name size for a variable as that shadows the function of the same name which is very frequently used in matlab.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!