what exactly norm(x) function do?

조회 수: 41 (최근 30일)
Manu Chaudhary
Manu Chaudhary 2022년 9월 21일
편집: James Tursa 2022년 9월 22일
Good Afternoon,
I have to convert a part of my matlab code into C++.
I would be great if you can explain me what exactly norm(x) do on a vector of complex number so that I can write a C++ code for it.
I just asked a similar question on stack overflow but could not get much response.
Thank you in advance.
  댓글 수: 5
Manu Chaudhary
Manu Chaudhary 2022년 9월 22일
Hi Steven,
Thank you updating me regarding the MATLAB coder. I will try to use this feature of MATLAB.
Manu Chaudhary
Manu Chaudhary 2022년 9월 22일
편집: Manu Chaudhary 2022년 9월 22일
The formula of norm(x) is defined as above on your website. Just for confirmation, |vk| means a^2 + b^2?

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

채택된 답변

James Tursa
James Tursa 2022년 9월 21일
편집: James Tursa 2022년 9월 21일
The equivalent function to norm( ) for a complex column vector x is
sqrt(x' * x)
where x' is the complex conjugate transpose. Essentially sqrt(dot(x,x)). Note that dot( ) does the complex conjugate part of the calculation automatically.
E.g.,
x = [1;2;3;4] + [5;6;7;8]*1i
x =
1.0000 + 5.0000i 2.0000 + 6.0000i 3.0000 + 7.0000i 4.0000 + 8.0000i
norm(x)
ans = 14.2829
sqrt(x'*x)
ans = 14.2829
sqrt(dot(x,x))
ans = 14.2829
So in C/C++ just run a loop to sum up the individual element-by-element multiplies for the dot product result. For an element of the form a+b*i you will be summing a^2+b^2 and then taking the sqrt( ) of the final sum.
  댓글 수: 4
Manu Chaudhary
Manu Chaudhary 2022년 9월 22일
Thank you James. I think now the conversion of norm x in c++ is correct.
int sum=0;
for(int i=0;i < data_size_val; i++){
sum= sum +pow(real_number[i],2)+ pow(img_number[i],2); // Need some correction Here
}
float normalized_value= sqrt(sum);
std::cout << "The normalized value" << normalized_value << std::endl;
James Tursa
James Tursa 2022년 9월 22일
편집: James Tursa 2022년 9월 22일
Yes this algorithm is correct, but I still advise changing your code to do the arithmetic in double to avoid potential pitfalls where the int calculations might overflow.

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

추가 답변 (1개)

Matt J
Matt J 2022년 9월 22일
편집: Matt J 2022년 9월 22일
Assuming you only care about p=2, If you already have code that computes norm(x) for real x, you can extend it to complex x via,
norm(x) = norm( [norm(real(x)), norm(imag(x)) ] )
which is easily verified below,
x=complex(rand(5,1), rand(5,1));
norm(x)
ans = 1.2548
norm( [norm(real(x)), norm(imag(x)) ] )
ans = 1.2548
Alternatively, if you have an implementation of abs, you could do norm(abs(x),p) which will work for any p-norm:
p=3;
norm(x,p)
ans = 1.0042
norm(abs(x),p)
ans = 1.0042

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by