Can this for loop be vectorized?

The for loop below runs over the third dimension (page) to fill the alpha array. Is it possible to vectorize this for loop?
alpha_i_h_t = NaN(N_i,N_t,N_draws);
for i = 1:N_draws
alpha_i_h_t(:,:,i) = e_i_h(:,i) + e_h * t;
end

2 件のコメント

Torsten
Torsten 2022 年 2 月 28 日
e_i_h(:,i) is a column vector. For e_i_h(:,i) + e_h * t to be defined, e_h * t must be a column vector or a scalar.
So how do you want to write a column vector into a matrix array alpha_i_h_t(:,:,i) ?
Bruno Luong
Bruno Luong 2022 年 2 月 28 日
No Torsen because MATLAB auto-expansion.

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

 採用された回答

Bruno Luong
Bruno Luong 2022 年 2 月 28 日
編集済み: Bruno Luong 2022 年 2 月 28 日

0 投票

e_i_h_reshape = reshape(e_i_h,N_i,1,N_draws);
alpha_i_h_t = e_i_h_reshape + e_h * t;

3 件のコメント

Snoopy
Snoopy 2022 年 2 月 28 日
Thank you very much.
Torsten
Torsten 2022 年 2 月 28 日
And why do you need a three-dimensional array for a two-dimensional content ?
Or do I get something wrong ?
Bruno Luong
Bruno Luong 2022 年 3 月 1 日
編集済み: Bruno Luong 2022 年 3 月 1 日
The result is 3D, each page contains a matrix sum of column by row vector.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 2 月 28 日

編集済み:

2022 年 3 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by