HELP PLEASE!! How do I print matlab "1:5 multiply increases each line"

This is my code
clc
fprintf (' SGD USD GBP EURO \n')
for (x= 1 : 5)
USD = 0.69 *2;
GBP = 0.49 *2;
EURO = 0.64 *2;
fprintf (' \n %d %.2f %.2f %.2f ' ,SGD ,USD ,GBP ,EURO);
end
My results
SGD USD GBP EURO
1 1.38 0.98 1.28
1 1.38 0.98 1.28
1 1.38 0.98 1.28
1 1.38 0.98 1.28
1 1.38 0.98 1.28 >>
I want the 2nd line to multiple by two, the thrid line to multiple by three... so on and so fourth...

 採用された回答

Stephen23
Stephen23 2016 年 1 月 18 日
編集済み: Stephen23 2016 年 1 月 18 日

0 投票

Without any loops, using vectorized code. Note that you can simply add new currencies to the arrays and it will work perfectly (i.e. changing the code itself is not required):
% the arrays:
name = {'SGD','USD','GBP','EURO'};
xchg = [ 1, 0.69, 0.49, 0.64];
mult = [1:5,10,20]; % multiples
% the code:
fprintf('%8s',name{:})
fprintf('\n')
fmt = repmat('%8.2f',1,numel(name));
fprintf([fmt,'\n'],xchg(:)*mult)
prints this:
SGD USD GBP EURO
1.00 0.69 0.49 0.64
2.00 1.38 0.98 1.28
3.00 2.07 1.47 1.92
4.00 2.76 1.96 2.56
5.00 3.45 2.45 3.20
10.00 6.90 4.90 6.40
20.00 13.80 9.80 12.80

3 件のコメント

Ken kaneki
Ken kaneki 2016 年 1 月 18 日
Oh god... It took me hours to do it and yet you completed it in few sec... I guess i need to work harder... Is there any simply way to do it? I don't understand some of the parts.... But still thanks for your answer!
Stephen23
Stephen23 2016 年 1 月 18 日
編集済み: Stephen23 2016 年 1 月 18 日
My pleasure. Here are a few tips to understand how my solution works:
  1. fprintf keeps repeating its format until it runs out of values to work with: this means that fprintf('%f,',1:3) prints '1,2,3,'.
  2. The minimum total width of the output can be set: fprintf('%4f',1) prints '___1' (three spaces are indicated here by three underscores).
  3. The product xchg(:)*mult calculates all values at once in a matrix. Try it in your command window!
Now you should be able to understand how it works. Of course the fprintf documentation explains how it behaves, that is where I learned how to do these kind of things.
Ken kaneki
Ken kaneki 2016 年 1 月 18 日
Thanks Stephen, i'm slowly digesting it hahaha! I guess it's gonna take awhile...

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

その他の回答 (1 件)

C.J. Harris
C.J. Harris 2016 年 1 月 18 日

1 投票

Do you mean like this?
clc
fprintf(' SGD USD GBP EURO \n')
for x= 1 : 5
USD = 0.69 * 2 * x;
GBP = 0.49 * 2 * x;
EURO = 0.64 * 2 * x;
SGD = x;
fprintf (' \n %d %.2f %.2f %.2f ' ,SGD ,USD ,GBP ,EURO);
end
Result:
SGD USD GBP EURO
1 1.38 0.98 1.28
2 2.76 1.96 2.56
3 4.14 2.94 3.84
4 5.52 3.92 5.12
5 6.90 4.90 6.40

1 件のコメント

Ken kaneki
Ken kaneki 2016 年 1 月 18 日
Thank you guys for your time and offered help to me, really really appreciated. The mistake i made was I didn't add the * x; !!

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

質問済み:

2016 年 1 月 18 日

編集済み:

2016 年 1 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by