I want to sum the loop result which the result is indicates the total of selected input

1 次查看(过去 30 天)
disp('Please vote for your candidates'); a=1; b=2; c=3; d=4;
for y=1:5
x=input('your candidates= ')
end
w = sum(a)
x = sum(b)
y = sum(c)
z = sum(d)

采纳的回答

Wanbin Song
Wanbin Song 2015-10-27
You can code it as belows:
disp('Please vote for your candidates'); a=1; b=2; c=3; d=4;
result = [];
for y=1:5
x = input('your candidates= ');
result = [result x];
end
w = nnz(result == a)
x = nnz(result == b)
y = nnz(result == c)
z = nnz(result == d)
Using vector concatenation and nnz(number of non-zero) function.

更多回答(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