フィルターのクリア

How to vectorize function on vectors stored in 3D tensor

4 ビュー (過去 30 日間)
Noya Linder
Noya Linder 2023 年 10 月 1 日
コメント済み: Dyuman Joshi 2023 年 10 月 3 日
Hi everyone.
I have a function that sums the 3rd dimension as presented here.
This is the code:
function sumeol = sum_over_line(tau, L, zstruct, rstruct, P)
before = zeros(P.Ntimes_An, rstruct.rlength);
for i=1:P.Ntimes_An
for j=1:rstruct.rlength
for k=1:zstruct.zlength-2
before(i, j) = before(i, j).*(1-1./(exp(tau(i, j, k)))) + L(i, j, k);
end
end
end
sumeol = before;
end
I would like to vectorize it - meaning getting rid of the loop over i and j and just having one loop so that the function will get the tensors tau and L sized aXbXc and return a matrix aXb.
How should I go about that? Thank you in advance
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 2 日
There was a misunderstanding on my side, Sorry. I have posted a new answer, please check it.

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

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 10 月 2 日
Try this -
function sumeol = sum_over_line(tau, L, zstruct, rstruct, P)
before = zeros(P.Ntimes_An, rstruct.rlength);
i=1:P.Ntimes_An;
j=1:rstruct.rlength;
for k=1:zstruct.zlength-2
before(i, j) = before(i, j).*(1-1./(exp(tau(i, j, k)))) + L(i, j, k);
end
sumeol = before;
end
  2 件のコメント
Noya Linder
Noya Linder 2023 年 10 月 3 日
Thank you so much!!
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 3 日
You are welcome!

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

その他の回答 (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