How can i make a specific program with structure 'for'?

I am required to write a program with structure'for' that asks me first to enter a vector then the program automatically adds the numbers in vector and give me a total such as exemple below: V=input('write the vector numbers'), then what to write to make it calculate the total of numbers that will be written and thanks.

回答 (1 件)

Stephen23
Stephen23 2016 年 9 月 3 日
編集済み: Stephen23 2016 年 9 月 3 日

0 投票

>> sum(sscanf(input('Enter a vector: ','s'),'%f%*[, ]'))
Enter a vector: 1,2,2,4
ans =
9
>> sum(sscanf(input('Enter a vector: ','s'),'%f%*[, ]'))
Enter a vector: 1.2 3.5
ans =
4.7

2 件のコメント

Salem Saber
Salem Saber 2016 年 9 月 4 日
thanks sir Stephen cobeldick but as i already stated that i am required to include a for structure in the program and thanks.
Stephen23
Stephen23 2016 年 9 月 5 日
>> vec = sscanf(input('Enter a vector: ','s'),'%f%*[, ]');
Enter a vector: 1,2,2,4
>> out = 0; for k = 1:numel(vec), out = out+vec(k); end
>> out
out = 9

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Simulink Check についてさらに検索

製品

質問済み:

2016 年 9 月 3 日

コメント済み:

2016 年 9 月 5 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by