creating a new array from a preexisting array

2 ビュー (過去 30 日間)
Rasmus Hvidberg
Rasmus Hvidberg 2019 年 3 月 4 日
コメント済み: Rasmus Hvidberg 2019 年 3 月 4 日
From the following code, I want matlab to create a new array, b.
Array a should be considered as pairs of real and imaginary parts of a number like A = [R1 I1 R2 I2 R3 I3 ...] where R1 is the real part of the complex number and I1 is the complex part of the number. I want array b to contain the magnitude of the complex numbers.
a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
b = zeros(length(a)/2)
for i = length(a)/2
b(i) = sqrt((a(i*2-1).^2) + (a(i*2).^2));
end
For some reason this creates an 8x8 array instead of a single array of 8 elements. I don't know why. Please help.
  1 件のコメント
KSSV
KSSV 2019 年 3 月 4 日
YOu should use:
b = zeros(1,length(a)/2)
This is wrong:
b(i) = sqrt((a(i*2-1).^2) + (a(i*2).^2));
What you are trying to do?

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

採用された回答

KSSV
KSSV 2019 年 3 月 4 日
May be your are looking for this:
a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
b = zeros(1,length(a)/2) ;
for i = 1:length(a)/2
b(i) = sqrt((2*a(i)-1).^2 + (2*a(i).^2));
end
  1 件のコメント
Rasmus Hvidberg
Rasmus Hvidberg 2019 年 3 月 4 日
While the expression for b(i) that you proposed doesn't do what I was looking for, the main problem was with how how i declared b in the 2nd line, apparently. Thanks for the answer :)

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by