複素数の事前割り当て

조회 수: 26 (최근 30일)
osamu
osamu 2024년 4월 8일 5:09
댓글: Dyuman Joshi 2024년 4월 9일 11:19
サイズが大きく、for文などでサイズが変動する配列の扱いについてはzerosなどを使って事前割り当てを行うことが以下のリンクで推奨されています。
これについて対象が複素数の場合はどのような事前割り当てをしたらいいでしょうか?
例えば以下のような式の場合zerosで定義したxに複素数を導入する方法は、目的の事前割り当ての効果を得られているのでしょうか。
x = zeros(1,1000000);
for k = 2:1000000
Real = k*5
Img = k*8
x(k) = complex(Real, Img)
end

채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 4월 8일 6:46
You can use this syntax of zeros() to preallocate the data as a complex array -https://in.mathworks.com/help/matlab/ref/zeros.html#d126e1907954
N = 1e5;
%syntax
x = zeros(1, N, 'like', 1i);
for k = 2:N
Real = k*5;
Img = k*8;
x(k) = complex(Real, Img);
end
x(2:5)
ans =
10.0000 +16.0000i 15.0000 +24.0000i 20.0000 +32.0000i 25.0000 +40.0000i
  댓글 수: 2
osamu
osamu 2024년 4월 9일 0:40
Thank you for your response.
I see that you can define the type of complex numbers using the zeros function.
I have gained a deeper understanding.
Thank you very much.
Dyuman Joshi
Dyuman Joshi 2024년 4월 9일 11:19
You are welcome! Glad to have helped :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 ループと条件付きステートメント에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!