creating a new array from a preexisting array
7 次查看(过去 30 天)
显示 更早的评论
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
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
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!