Element-wise Complex Magnitude Calculation

29 ビュー (過去 30 日間)
Gee
Gee 2019 年 7 月 15 日
コメント済み: Raj 2019 年 7 月 16 日
I have an array of complex numbers (here is a snippet):
1273 + 1513i
1271 + 1481i
1256 + 1432i
1240 + 1347i
1210 + 1183i
1190 + 1029i
1169 + 870i
1156 + 714i
I'm aware the abs() function returns the complex magnitude of the vector from the origin 0,0 for each element in the vector, however is it possible to calculate the complex magnitude in an element-wise manner such that the returned value is the absolute length of all the elements in the complex vector with each previous element being treated as the origin rather than 0,0?
  3 件のコメント
Gee
Gee 2019 年 7 月 15 日
Thank you so much!
Bruno Luong
Bruno Luong 2019 年 7 月 15 日
or
abs(diff(z))

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

回答 (1 件)

Raj
Raj 2019 年 7 月 15 日
Why don't you just put it in a loop like this:
A = rand(5,1) + i*rand(5,1); % Assume this is your array
B=zeros(length(A),1);
B(1,1)=abs(A(1));
for ii=2: length(A)
B(ii,1)=abs(A(ii)-A(ii-1));
end
B is the matrix you need.
P.S.: There may be better way of doing this also.
  2 件のコメント
Guillaume
Guillaume 2019 年 7 月 15 日
編集済み: Guillaume 2019 年 7 月 15 日
"P.S.: There may be better way of doing this also."
As shown by Bruno, the whole lot can be replaced by
B = abs(diff(B);
You never need a loop to calculate the difference between consecutive elements in matlab.
Raj
Raj 2019 年 7 月 16 日
@Guillaume Yes i got it. Thanks. I was preoccupied in something and was not thinking straight.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by