how can i format result of loop ?

1 回表示 (過去 30 日間)
work wolf
work wolf 2022 年 6 月 20 日
コメント済み: Walter Roberson 2022 年 6 月 21 日
how can i format output2 in the following code by using for loop
f=@(x) x.^2;
x= [0.1 0.3 0.5 0.6 0.66 0.9 1];
for i =1:length(x)
output1{i}= f(x(i))';
end
output2=[ f(x(1))' f(x(2))' f(x(3))' f(x(4))' f(x(5))' f(x(6))' f(x(7))'];
%---------------------------------
output1{1:length(x)}
output2
result:
ans =
0.0100
ans =
0.0900
ans =
0.2500
ans =
0.3600
ans =
0.4356
ans =
0.8100
ans =
1
output2 =
0.0100 0.0900 0.2500 0.3600 0.4356 0.8100 1.0000
Note:
1.The differnent between output1 & output2, formatting
2. The previous code is brief for 7 digit (size(7,1), so easy to write manually. So, i want using for loop for large size(n,m).
  4 件のコメント
Walter Roberson
Walter Roberson 2022 年 6 月 20 日
is there a particular reason why you are using cell array to record the output of f(x)?
Walter Roberson
Walter Roberson 2022 年 6 月 21 日
f=@(x) x.^2;
x= [0.1 0.3 0.5 0.6 0.66 0.9 1]
x = 1×7
0.1000 0.3000 0.5000 0.6000 0.6600 0.9000 1.0000
output1 = arrayfun(f, x)
output1 = 1×7
0.0100 0.0900 0.2500 0.3600 0.4356 0.8100 1.0000

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

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