フィルターのクリア

Array using for loop: using sprintf

5 ビュー (過去 30 日間)
Ullas Rajvanshi
Ullas Rajvanshi 2016 年 9 月 12 日
コメント済み: Stephen23 2016 年 9 月 12 日
Make the following two vectors, each with the same number of elements (77). v1: a 1D-array with random numbers (floats) between 0 and 8515.0 . v2: the nearest integer value corresponding to every element of v1. now use the sprintf() command to print a table with 77 rows and 3 columns. The first column contains v2 (displayed as integers, so 41 instead of 41.000). The second column contains v1, displayed as floats with 2 decimals. The third column contains v1 displayed in scientific notation, with 3 decimals. I know we have to make use of for loop in this, but how?
Thanks in advance
  1 件のコメント
Stephen23
Stephen23 2016 年 9 月 12 日
@Ullas Rajvanshi: what have you tried so far?
If we do you work for you, how do you expect to learn anything from that course?

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

採用された回答

Stephen23
Stephen23 2016 年 9 月 12 日
編集済み: Stephen23 2016 年 9 月 12 日
You could use a loop like this:
  1. define the two vectors, say X and Y.
  2. loop over them, using for k = 1:numel(X).
  3. use indexing each loop to access the vector elements
  4. call sprintf with those values.
But actually you don't need to use a loop at all. Have a look at this:
>> M = 9*rand(1,5);
>> fprintf('%4.0f,%7.2f,%12.3e\n',[M;M;M])
7, 6.82, 6.820e+00
7, 6.69, 6.688e+00
4, 3.53, 3.530e+00
6, 5.90, 5.899e+00
2, 1.54, 1.541e+00
  3 件のコメント
Ullas Rajvanshi
Ullas Rajvanshi 2016 年 9 月 12 日
Thanks for the motivation. I would like to apologise for that. I am able to solve it now. Thanks for your help! :)
Stephen23
Stephen23 2016 年 9 月 12 日
@Ullas Rajvanshi: I am glad that you got motivated. I really do wish you lots of luck and fun learning MATLAB! And please come and ask questions when something does not work for you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by