AVOID FOR LOOP for changing size vector

4 ビュー (過去 30 日間)
gabriele fadanelli
gabriele fadanelli 2020 年 3 月 27 日
回答済み: Abhisek Pradhan 2020 年 3 月 30 日
I need to solve this problem without a for loop:
B= (1:20)
A = [];
A(1) = 1/(1+B(1));
for k = 2:length(B)
A(k,1) = (1-B(k)*sum(A))/(1+B(k));
end
i.e. I need to know if it is possible to get information from previous calculation to create a vector, but without a for loop. Thanks.
  2 件のコメント
Adam
Adam 2020 年 3 月 27 日
Can't think if there is a way to avoid a for loop for that, but the vector size certainly does not need to change. You can presize A as
A = zeros( size( B ) );
gabriele fadanelli
gabriele fadanelli 2020 年 3 月 27 日
編集済み: gabriele fadanelli 2020 年 3 月 27 日
Thank you! Actually I was trying to change the for loop at first.

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

採用された回答

Abhisek Pradhan
Abhisek Pradhan 2020 年 3 月 30 日
I could understand you want to avoid use of loop. This can be done using vectorization.
Refer the following link which has few examples on how to do vectorization.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by