How can I get a vector from users?
469 次查看(过去 30 天)
显示 更早的评论
I need to get a vector from user. But user should enter the values one by one (eg: 1st value is 25 2nd value is 22). When user enter the all values (n^2 values), I want to see the vector. I tried something but it is not really work. Please, give me some info about that. ****vector seems like that, vector = [45 10 2 20].* *
n=input('num of value')
costs=zeros(1,n^2);
for i=1:n^2
vector=input('enter the %d\n th value',i)
end
disp(vector)
1 个评论
回答(2 个)
Image Analyst
2018-11-11
Try this:
% Enter as a string with numbers separted by commas or spaces
userResponse = input('enter the costs ', 's')
% Convert any commas to spaces.
userResponse = strrep(userResponse, ',', ' ')
% Convert strings to numbers.
numbers = sscanf(userResponse, '%f')
5 个评论
Image Analyst
2018-11-12
You can do that with the way I gave you. Most good MATLAB programmers would not do it the way your originally said.
Ali Baran Özpolat
2020-3-29
I couldn't understand your question, clearly. Do you mean to create a code that calculate the power of numbers and put in a vector? But, if you want to create your own vector, please try this;
m = input('m= ');
for i= 1:1
for j=1:m
a(i,j)= input('elements= ');
v=[a]
end
end
In this code, you can create your own vector by prompt "m" value. For example, if you type m= 5, you will have 1x5 vector. Then, you can start to prompt your own integers. If you want to create a matrix with the same method, you use this code:
n = input('n= ');
m = input('m= ');
for i= 1:n
for j=1:m
a(i,j)= input('elements= ')
end
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!