creating a new array from a preexisting array

7 views (last 30 days)
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 Comment
KSSV
KSSV on 4 Mar 2019
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?

Sign in to comment.

Accepted Answer

KSSV
KSSV on 4 Mar 2019
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 Comment
Rasmus Hvidberg
Rasmus Hvidberg on 4 Mar 2019
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 :)

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!